本文介绍了如何使用Apache Client 4.5.5处理无效的SSL证书?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Apache Client 4.5.5连接到我的Web服务器之一.
I am trying to use Apache Client 4.5.5 to connect to one of my web server.
我正在尝试使用SSLContextBuilder,但现在似乎已经弃用了它,我不想使用已弃用的东西.
I am trying to use SSLContextBuilder but it seems its deprecated now, I don't want to use deprecated.
有人可以建议处理无效证书的最新方法是什么吗?
Can someone suggest what are the latest way to handle Invalid certification?
下面是我的代码,它可以正常工作,但是不建议使用SSLContext,SSLContextBuilder和loadTrustmaterial.
Below is my code it works fine but SSLContext, SSLContextBuilder, loadTrustmaterial are deprecated.
SSLContextBuilder sscb = new SSLContextBuilder();
sscb.loadTrustMaterial(null, new org.apache.http.conn.ssl.TrustSelfSignedStrategy());
SSLContext sslContext = sscb.build();
SSLConnectionSocketFactory sslSocketFactory =
new SSLConnectionSocketFactory(sslContext, new org.apache.http.conn.ssl.DefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setSSLSocketFactory(sslSocketFactory)
.setConnectionManager(connectionMgr)
.build();
try {
HttpPost httppost = new HttpPost("myURL");
推荐答案
Apache HttpClient 4.5.5
HttpClient httpClient = HttpClients
.custom()
.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
这篇关于如何使用Apache Client 4.5.5处理无效的SSL证书?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!