我们正在尝试将 httpclient 从 4 升级到 5。作为升级的一部分,我们相应地更改了导入。但是,代码使用 Httpclientbuilder 并设置 sslcontext。 根据 apache 的文档,他们已从 Httpclientbuilder 方法中删除了 setsslcontext,但我没有找到任何替代方案。
错误提示 HttpClientBuilder 类型未定义 setSSLContext(sslcontext) 方法。
代码如下:
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
SSLContext sslcontext = SSLContext.getDefault();
HttpClient httpclient = HttpClientBuilder.create().setSSLContext(sslcontext).build();
ClientHttpRequestFactory reqFac = new HttpComponentsClientHttpRequestFactory(httpclient);
这是我们使用的替代品:
HttpClientBuilder clientBuilder = HttpClients.custom();
final SSLContext sslContext = createSslContext();
final ConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier());
final Registry<ConnectionSocketFactory> socketFactoryRegistry =
RegistryBuilder.<ConnectionSocketFactory> create()
.register("https", sslsf)
.register("http", new PlainConnectionSocketFactory())
.build();
final BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);
clientBuilder.setConnectionManager(connectionManager);