java – 在Apache HttpClient 4.3中忽略SSL证书

如何忽略Apache HttpClient 4.3的SSL证书(全部信任)?

我找到的所有答案处理以前的版本,并改变了API。

有关:

> How to ignore SSL certificate errors in Apache HttpClient 4.0
> How to handle invalid SSL certificates with Apache HttpClient?
> Need to trust all the certificates during the development using Spring
> Ignore SSL Certificate Errors with Java

编辑:

>它仅用于测试目的。孩子们,不要在家里(或在生产中)

以下代码适用于信任自签名证书。您必须在创建客户端时使用TrustSelfSignedStrategy

    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
            builder.build());
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(
            sslsf).build();

    HttpGet httpGet = new HttpGet("https://some-server");
    CloseableHttpResponse response = httpclient.execute(httpGet);
    try {
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
    }
    finally {
        response.close();
    }

我没有包括SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER目的:关键是允许测试自签名证书,所以你不必从证书颁发机构获取一个正确的证书。您可以轻松地使用正确的主机名创建自签名证书,因此无需添加SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER标志。

http://stackoverflow.com/questions/19517538/ignoring-ssl-certificate-in-apache-httpclient-4-3

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:java – 在Apache HttpClient 4.3中忽略SSL证书