日期:2014-05-16  浏览次数:21077 次

Apache HttpClient 4.3-忽略证书问题

在浏览器访问某些网站时提示证书有问题,则用Apache HttpClient 4.3访问时会出现SSL错误,无法访问网站。

解决方法如下,允许信任自签证书,即信任所有证书。

?

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();
    }

?

方法转自:http://stackoverflow.com/questions/19517538/ignoring-ssl-certificate-in-apache-httpclient-4-3?lq=1