爬取网页遇到的目标站点证书不合法问题。

使用jsoup爬取解析网页时,出现了如下的异常情况。

  1. 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
  2. at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
  3. at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1627)
  4. at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:204)
  5. at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:198)
  6. at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:994)
  7. at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:142)
  8. at sun.security.ssl.Handshaker.processLoop(Handshaker.java:533)
  9. at sun.security.ssl.Handshaker.process_record(Handshaker.java:471)
  10. at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:904)
  11. at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1132)
  12. at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:643)
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
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1627)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:204)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:198)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:994)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:142)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:533)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:471)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:904)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1132)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:643)
查明是无效的SSL证书问题。由于现在很多网站由http全站升级到https,可能是原站点SSL没有部署好,导致证书无效,也有可能是其证书本身就不被认可。对于爬取其网页就会出现证书验证出错的问题。
对于使用Jsoup自带接口来下载网页的,最新版本的1.9.2有validateTLSCertificates(boolean false)接口即可。
  1. Jsoup.connect(url).timeout(30000).userAgent(UA).validateTLSCertificates(false).get()
Jsoup.connect(url).timeout(30000).userAgent(UA).validateTLSCertificates(false).get()
java默认的证书集合里面不存在对于多数自注册的证书,对于不使用第三方库来做http请求的话,我们可以手动
创建TrustManager 来解决。确定要建立的链接的站点,否则不推荐这种方式
  1. public static InputStream getByDisableCertValidation(String url) {
  2. TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
  3. public X509Certificate[] getAcceptedIssuers() {
  4. return new X509Certificate[0];
  5. }
  6. public void checkClientTrusted(X509Certificate[] certs, String authType) {
  7. }
  8. public void checkServerTrusted(X509Certificate[] certs, String authType) {
  9. }
  10. } };
  11. HostnameVerifier hv = new HostnameVerifier() {
  12. public boolean verify(String hostname, SSLSession session) {
  13. return true;
  14. }
  15. };
  16. try {
  17. SSLContext sc = SSLContext.getInstance(”SSL”);
  18. sc.init(null, trustAllCerts, new SecureRandom());
  19. HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  20. HttpsURLConnection.setDefaultHostnameVerifier(hv);
  21. URL uRL = new URL(url);
  22. HttpsURLConnection urlConnection = (HttpsURLConnection) uRL.openConnection();
  23. InputStream is = urlConnection.getInputStream();
  24. return is;
  25. } catch (Exception e) {
  26. }
  27. return null;
  28. }
public static InputStream getByDisableCertValidation(String url) {
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} }; HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}; try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv); URL uRL = new URL(url);
HttpsURLConnection urlConnection = (HttpsURLConnection) uRL.openConnection();
InputStream is = urlConnection.getInputStream();
return is;
} catch (Exception e) {
}
return null;
}

refer:

http://snowolf.iteye.com/blog/391931

http://stackoverflow.com/questions/1828775/how-to-handle-invalid-ssl-certificates-with-apache-httpclient

Jsoup访问https网址异常SSLHandshakeException:

解决方式:

Jsoup.connect(url)
.timeout(30000)
.userAgent(UA)
.validateTLSCertificates(false)
.get()

原文地址:http://blog.csdn.net/louxuez/article/details/52814538

感谢原作者的分享,谢谢。如有侵犯,请联系笔者删除。QQ:337081267

Jsoup访问https网址异常SSLHandshakeException(已解决)的更多相关文章

  1. sun X509/X500Name异常(已解决)

    appium环境搭建好后,再跑第一个脚本时遇到这个问题: Errors occurred during the build.Errors running builder 'Android Packag ...

  2. C# webkit 内核浏览器 访问https 网站 提示 Problem with the SSL CA cert (path? access rights?)

    C# webkit 内核浏览器 访问https 网站 提示 Problem with the SSL CA cert (path? access rights?) 解决方法: 陈凯文11112014- ...

  3. Nginx. 用http访问https跨域

    用http 访问 https域名, 报跨越问题 解决方法: 在nginx相应服务的转发配置下添加: add_header 'Access-Control-Allow-Origin' 'http://i ...

  4. 解决访问HTTPS,抛出的异常javax.net.ssl.SSLHandshakeException

    本地测试没问题,http换成https抛出异常javax.net.ssl.SSLHandshakeException,网上有说是服务器证书,有说要启动SSL3协议的,反正没有找到有用的. 在GET和P ...

  5. AFNetworking 原作者都无法解决的问题: 如何使用ip直接访问https网站?

    背景 最近App似乎有报异常是DNS无法解析,尝试解决此问题.搜集到的资料很少,甚至连AFN原作者都判定这可能是一个无解的问题,参见: https://github.com/AFNetworking/ ...

  6. sqlserver,执行生成脚本时“引发类型为“System.OutOfMemoryException”的异常”(已解决)

    sqlserver,执行生成脚本时“引发类型为“System.OutOfMemoryException”的异常”(已解决) 出现此错误主要是因为.sql的脚本文件过大(一般都超过100M)造成内存无法 ...

  7. 源码编译安装 PHP5.5.0,解决curl_exec访问HTTPS返回502错误的问题(修改PATH路径)

    最近碰到一个奇怪的问题, PHP使用 curl_exec 访问 HTTPS 网页时, 返回502错误, 访问HTTP网页时没有问题,  用   echo   phpinfo() ;  查看, 支持op ...

  8. 解决python2.7.9以下版本requests访问https的问题

    在python2.7.9以下版本requests访问https连接后,总会报一些关于SSL warning. 解决法子可以参考:https://urllib3.readthedocs.io/en/la ...

  9. 解决Chrome浏览器访问https提示“您的连接不是私密连接”的问题

    安装fiddler后,使用Chrome访问https网站时,可能会出现以下错误,本文说明如何解决此类问题: “您的连接不是私密连接”.“NET::ERR_CERT_AUTHORITY_INVALID” ...

随机推荐

  1. 常用URL分享,实用地址

    常用地址 文库文档免费下载地址1:http://www.hiwenku.com/ 文库文档免费下载下载2:http://www.20009.net/wk.html google地图拾取器:http:/ ...

  2. 05 div的嵌套

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Storm 学习之路(七)—— Storm集成 Redis 详解

    一.简介 Storm-Redis提供了Storm与Redis的集成支持,你只需要引入对应的依赖即可使用: <dependency> <groupId>org.apache.st ...

  4. 最全java多线程总结3——了解阻塞队列和线程安全集合不

      看了前两篇你肯定已经理解了 java 并发编程的低层构建.然而,在实际编程中,应该经可能的远离低层结构,毕竟太底层的东西用起来是比较容易出错的,特别是并发编程,既难以调试,也难以发现问题,我们还是 ...

  5. HTTP协议之应用

    通过对http协议的理解.我们可以根据这些特性来进行一些应用. 1.我们可以根据http请求的头信息refer信息,我们可以来做网站的防盗链.refer记录访问到目标网站的上次访问路径.这样我们可以来 ...

  6. python面试题(二)字符串常用函数

    今天在微信的公众号上看到了一遍python学习开发的文章,里面有一些python的面试题,碰巧最近python不知道学什么了,索性学一下这篇文章啊!!先写一下一些字符串的常用函数.(ps:本人太菜,若 ...

  7. Filebeat 7.1.1 安装及使用(连接ES)

    1. 下载 & 解压 # 下载 wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.1.1-linux- ...

  8. 分布式个人理解概述和dubbo实现简述

    什么是分布式?为什么使用分布式? 个人有一些浅薄的理解希望可以批评指正,从概念和应用 两个方面概述:      一.概念:分布式也叫分布式服务,也就是说 他是 一种面向服务思想的程序设计和架构风格,典 ...

  9. 2. 2.1查找命令——linux基础增强,Linux命令学习

    2.1.查找命令 grep命令 grep 命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并 把匹配的行打印出来. 格式: grep [option] pattern [file] 可使用 ...

  10. Spring入门配置(一) - IOC

    一.初始命名空间配置 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="h ...