Java CloseableHttpClient不';不要使用sslcontext
我使用PoolighttpClientConnectionManager,并且我需要在每个请求上使用特定的sslcontext。 默认情况下,CloseableHttpClient使用manager的sslcontext,但我需要.setSSLContext(context)中的sslcontext。 如何解决这个问题? 我需要连接池,同时我需要每个请求的特定sslcontextJava CloseableHttpClient不';不要使用sslcontext,java,connection-pooling,sslcontext,Java,Connection Pooling,Sslcontext,我使用PoolighttpClientConnectionManager,并且我需要在每个请求上使用特定的sslcontext。 默认情况下,CloseableHttpClient使用manager的sslcontext,但我需要.setSSLContext(context)中的sslcontext。 如何解决这个问题? 我需要连接池,同时我需要每个请求的特定sslcontext CloseableHttpClient client = HttpClients.custom()
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(httpPoolManager.getConnectionManager())
.setSSLSocketFactory(new SSLConnectionSocketFactory(context, NoopHostnameVerifier.INSTANCE))
.setSSLContext(context)
.build();
setExternalRequestId(externalRequestId);
setHttpClient(client);
那些日子我一直在做这件事 您可以使用以下代码构建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);
我不得不深入研究源代码,但发现了以下工作,假设您可以构建自己的
poollighttpclientconnectionmanager
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();
Registry Registry=RegistryBuilder.create()
.register(“https”,新的SSLConnectionSocketFactory(sslContext))
.build();
CloseableHttpClient httpClient=HttpClientBuilder.create()
//重要一行--在构造函数中使用注册表
.setConnectionManager(新的池连接管理器(注册表))//重要
.build();
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();