gpt4 book ai didi

java - 如何忽略 Apache HttpComponents HttpClient 5.1 中的 SSL 证书错误

转载 作者:行者123 更新时间:2023-12-04 22:35:13 25 4
gpt4 key购买 nike

如何绕过 Apache HttpComponents HttpClient 5.1 的证书验证错误?
我找到了 working solution绕过 HttpClient 4.5 中建议自定义 HttpClient 的此类错误实例:

HttpClient httpClient = HttpClients
.custom()
.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
但它不适用于 HttpClient 5.1,如 setSSLContextsetSSLHostnameVerifier HttpClientBuilder 中不存在方法( HttpClients.custom() 返回)。

最佳答案

HC 5.1 中有几个专门的构建器可以用来做同样的事情:

CloseableHttpClient httpclient = HttpClients.custom()
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
.setSslContext(SSLContextBuilder.create()
.loadTrustMaterial(TrustAllStrategy.INSTANCE)
.build())
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build())
.build())
.build();

关于java - 如何忽略 Apache HttpComponents HttpClient 5.1 中的 SSL 证书错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69375468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com