异常解决:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.cer
Posted asas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了异常解决:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.cer相关的知识,希望对你有一定的参考价值。
前几天用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.cer的主要内容,如果未能解决你的问题,请参考以下文章