我使用 PoolingHttpClientconnectionManager ,我需要在每个请求上使用特定的sslcontext。 默认情况下,CloseableHttpClient使用manager的sslcontext,但我需要来自.setSSLContext(context)的 sslcontext。 怎么解决这个问题? 我需要连接池,同时我需要在每个请求上使用特定的sslcontext
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(httpPoolManager.getConnectionManager())
.setSSLSocketFactory(new SSLConnectionSocketFactory(context, NoopHostnameVerifier.INSTANCE))
.setSSLContext(context)
.build();
setExternalRequestId(externalRequestId);
setHttpClient(client);
答案 0 :(得分:0)
那些日子我正在努力。
您可以使用以下代码构建HTTPs客户端:
SSLContextBuilder builder = new SSLContextBuilder();
// Truest all request
try {
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), new String[] {"TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register(HTTPS, sslsf)
.build();
PoolingHttpClientConnectionManager pccm = new PoolingHttpClientConnectionManager(registry);
然后:
HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setConnectionManager(pccm)
.setConnectionManagerShared(true)
.build();
答案 1 :(得分:0)
我不得不仔细研究源代码,但是找到了以下作品,并假设您可以构建自己的PoolingHttpClientConnectionManager
。
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", new SSLConnectionSocketFactory(sslContext))
.build();
CloseableHttpClient httpClient = HttpClientBuilder.create()
// important line -- use registry in constructor
.setConnectionManager(new PoolingHttpClientConnectionManager(registry)) // IMPORTANT
.build();