httpclient支持https
只需要我们在创建CloseableHttpClient的时候支持ssl 就可以了。
举个例子:
原来创建CloseableHttpClient
private static CloseableHttpClient HTTP_CLIENTS;
//httpRequestRetryHandler 重试策略我就忽略了
static{
cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(MAX_TOTAL_CONNECTIONS);
// 默认设置为2
cm.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS);
// 客户端请求的默认设置
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(SOCKET_TIMEOUT)
.setConnectTimeout(CONNECT_TIMEOUT)
.setConnectionRequestTimeout(CONNECT_TIMEOUT)
.setRedirectsEnabled(false)
.setCookieSpec(CookieSpecs.STANDARD_STRICT)
.build();
httpclient = HttpClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(defaultRequestConfig)
.setRetryHandler(httpRequestRetryHandler)
.build();
}
举个例子:
现在创建CloseableHttpClient(支持https)
//httpRequestRetryHandler 重试策略我就忽略了
static{
cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(MAX_TOTAL_CONNECTIONS);
// 默认设置为2
cm.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS);
// 客户端请求的默认设置
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(SOCKET_TIMEOUT)
.setConnectTimeout(CONNECT_TIMEOUT)
.setConnectionRequestTimeout(CONNECT_TIMEOUT)
.setRedirectsEnabled(false)
.setCookieSpec(CookieSpecs.STANDARD_STRICT)
.build();
// https 信任所有
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
httpclient = HttpClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(defaultRequestConfig)
.setRetryHandler(httpRequestRetryHandler)
//https
.setSSLSocketFactory(sslSocketFactory)
.build();
}