如何使用Apache客户端4.5.5处理无效的SSL证书?

时间:2018-03-19 16:43:32

标签: java apache apache-commons-httpclient

我正在尝试使用Apache Client 4.5.5连接到我的一个Web服务器。

我正在尝试使用SSLContextBuilder,但它现在似乎已被弃用,我不想使用已弃用。

有人可以建议处理无效认证的最新方法是什么?

下面是我的代码,它运行正常,但不推荐使用SSLContext,SSLContextBuilder,loadTrustmaterial。

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");

1 个答案:

答案 0 :(得分:4)

Apache HttpClient 4.5.5

HttpClient httpClient = HttpClients
            .custom()
            .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
            .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
            .build();