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測試視頻 |

                    sun.security.validator.ValidatorException: PKIX path building failed

                    作者:數據無憂   時間:2020-09-18 13:24:53
                    Java做爬蟲處理HTTPS鏈接時報錯sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

                    這個是因為程序未處理證書才會報這個錯誤,解決辦法只需要信任證書即可。添加下面的工具類:

                    // 信任所有證書    
                    public class TrustCertsUtil {
                    public static void trustAllHttpsCertificates() {
                    // 如果爬蟲請求HTTPS網址,必須加入這兩行
                    System.setProperty("jdk.http.auth.proxying.disabledSchemes", "");
                    System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
                    try {
                           javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
                           javax.net.ssl.TrustManager tm = new MITM();
                           trustAllCerts[0] = tm;
                           javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("TLS");
                           sc.init(null, trustAllCerts, null);
                           javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                    } catch (Exception e) {
                    System.err.println("【DATA5U】設置證書出錯,原因:" + e.getMessage());
                    }
                    }
                       static class MITM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
                           public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                               return null;
                           }
                           public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
                               return true;
                           }
                           public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
                               return true;
                           }
                           public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
                                   throws java.security.cert.CertificateException {
                               return;
                           }
                           public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
                                   throws java.security.cert.CertificateException {
                               return;
                           }
                       }
                    }


                    然后再項目的main方法中加入這行代碼即可:TrustCertsUtil.trustAllHttpsCertificates();

                    如果項目使用的是HttpClient,上面的方法還不見效,請參考  HttpClient跳過證書的方法


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

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