哈希塞特 程序员  2020年07月13日
HttpClient访问https
帮忙看下我这段配置,为什么不能访问https的接口

static {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
SSLContext sc;
try {
sc = SSLContext.getInstance("TLS");
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager(){
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
sc.init(null, trustAllCerts, null);
} catch (Exception e) {
LOG.error("some thing happen", e);
throw new ServiceException(e);
}
ConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sc, (s, sslSession) -> true);
ConnectionKeepAliveStrategy connectionKeepAliveStrategy = (httpResponse, httpContext) -> {
return 20L * 1000;
};
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", plainsf).register("ftp", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
connManager.setMaxTotal(400);
connManager.setDefaultMaxPerRoute(200);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(HTTP_CONNECT_TIME_OUT)
.setConnectionRequestTimeout(3000).setSocketTimeout(HTTP_TIME_OUT).build();
client = HttpClients.custom().setConnectionManager(connManager)
.setRetryHandler(new DefaultHttpRequestRetryHandler()).setDefaultRequestConfig(requestConfig)
.setKeepAliveStrategy(connectionKeepAliveStrategy).build();
}
...全文
1100 点赞 收藏 7
写回复
7 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Web 开发
创建于2007-09-28

5.2w+

社区成员

34.1w+

社区内容

Java Web 开发
社区公告
暂无公告