标签: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(); }
```` }
``
标签:led hostname uil soc mamicode uri ring port nbsp
原文地址:https://www.cnblogs.com/asas/p/12685836.html