码迷,mamicode.com
首页 > 其他好文 > 详细

异常解决:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

时间:2020-04-12 16:47:40      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:led   hostname   uil   soc   mamicode   uri   ring   port   nbsp   

前几天用JSOUP写爬虫Demo时,遇到这个异常

技术图片

百度了一番原来是因为目标站点启用了HTTPS  而缺少安全证书时出现的异常,大概解决办法有2种:

  1. 手动导入安全证书(嫌麻烦 没使用);

  2. 忽略证书验证。

    相对于来说简单一点,在发起请求前调用这个方法,问题解决。

 

``` java

 

// 包不要导错了
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* 信任任何站点,实现https页面的正常访问
*/

public static void trustEveryone() {

try {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new X509TrustManager[]{new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        }}, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
```` }

``

 

异常解决:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

标签:led   hostname   uil   soc   mamicode   uri   ring   port   nbsp   

原文地址:https://www.cnblogs.com/asas/p/12685836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!