1. <rp id="c4hsd"></rp>
          1. <button id="c4hsd"><acronym id="c4hsd"><input id="c4hsd"></input></acronym></button>
          2. <tbody id="c4hsd"></tbody>

            <dd id="c4hsd"><pre id="c4hsd"></pre></dd>

              1. <th id="c4hsd"></th>
              2. <s id="c4hsd"><object id="c4hsd"></object></s>

                    [ 登錄 ] - [ 注冊 ] | 代碼示例DEMO | IP測試視頻 |

                    httpclient 4.5 https請求跳過證書驗證

                    作者:數據無憂   時間:2020-09-18 13:24:53
                    再使用代理IP抓取HTTPS網站的數據過程中,報錯:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

                    解決方案:信任所有證書,并通過SslUtil.SslHttpClientBuild()方法獲取CloseableHttpClient對象:

                    public class SslUtil {
                    public static CloseableHttpClient SslHttpClientBuild() {
                    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", trustAllHttpsCertificates()).build();
                    //創建ConnectionManager,添加Connection配置信息
                    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
                    CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
                    return httpClient;
                    }
                    private static SSLConnectionSocketFactory trustAllHttpsCertificates() {
                    SSLConnectionSocketFactory socketFactory = null;
                    TrustManager[] trustAllCerts = new TrustManager[1];
                    TrustManager tm = new miTM();
                    trustAllCerts[0] = tm;
                    SSLContext sc = null;
                    try {
                    sc = SSLContext.getInstance("TLS");
                    sc.init(null, trustAllCerts, null);
                    socketFactory = new SSLConnectionSocketFactory(sc, NoopHostnameVerifier.INSTANCE);
                    // HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                    } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                    } catch (KeyManagementException e) {
                    e.printStackTrace();
                    }
                    return socketFactory;
                    }
                    static class miTM implements TrustManager, X509TrustManager {
                    public X509Certificate[] getAcceptedIssuers() {
                    return null;
                    }
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    //don't check
                    }
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    //don't check
                    }
                    }
                    }


                    無憂代理IP(www.aooseo.com)原創文章,轉載請注明出處。

                    電話:4007-745-096
                    QQ:
                    周一至周日8:30-18:00 技術部電話熱線
                    久久夜色精品国产噜噜亚洲AV_老妇女性较大毛片_888亚洲欧美国产va在线播放_超碰人人透人人爽人人看