如何使用Apache HttpComponents HttpClient 5.1绕过证书验证错误
我在HttpClient 4.5中找到了一个可以绕过此类错误的working solution,它建议自定义HttpClient
实例:
HttpClient httpClient = HttpClients
.custom()
.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
但它不适用于HttpClient 5.1,因为setSSLContext
和setSSLHostnameVerifier
方法在HttpClientBuilder
( HttpClients.custom()
返回)中不存在。
发布于 2021-09-29 20:14:15
在HC5.1中有几个专门的构建器可以用来做同样的事情:
CloseableHttpClient httpclient = HttpClients.custom()
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
.setSslContext(SSLContextBuilder.create()
.loadTrustMaterial(TrustAllStrategy.INSTANCE)
.build())
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build())
.build())
.build();
https://stackoverflow.com/questions/69375468
复制相似问题