Java爬虫https网页内容报错SSLHandshakeException信任(忽略)所有SSL证书
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
原因:https出现信任弹出(访问网页时候弹出是否信任)
解决方案:忽略ssl证书
创建一个类忽略ssl证书
TrustSSL.java
import java.io.*;
import java.net.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.*; public class TrustSSL { private static class TrustAnyTrustManager implements 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[] {};
}
} private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
public static InputStream HttpsSSL(URL strUrl){ try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },new java.security.SecureRandom());
HttpsURLConnection conn = (HttpsURLConnection) strUrl.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
//设置超时间为5秒
conn.setConnectTimeout(5 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
conn.connect();
//获取服务器响应代码
int responsecode = conn.getResponseCode();
if (responsecode == 200) {
//得到输入流
return conn.getInputStream();
} else {
System.out.println("获取不到 " + strUrl + " 源码,服务器响应代码为:" + responsecode);
return null;
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return null; }
public static void main(String[] args) throws Exception {
HttpsSSL(new URL("url"));
}
}
调用:

成功获取网页内容
Java爬虫https网页内容报错SSLHandshakeException信任(忽略)所有SSL证书的更多相关文章
- Https协议报错:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl解决方法
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/82220333 所用应用服务器:JBoss服务 ...
- python https请求报错:SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED]
python爬虫,使用requests库发送https请求报错:SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 解决方法: imp ...
- 用java运行Hadoop程序报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.
用java运行Hadoop例程报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.所写代码如下: package ...
- Python使用requests模块访问HTTPS网站报错`certificate verify failed`
使用requests模块访问HTTPS网站报错: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Nam ...
- git https 请求报错 504
git https 请求报错 504 原因可能是因为设置了代理,ubuntu/deepin 系统可以检查 /etc/profile ~/.bashrc 内有没有设置 https 的代理. 有的话,去掉 ...
- java连接数据库时的报错
//java连接数据库时的报错 1 package Java数据库编程; import java.sql.DriverManager; import java.sql.SQLException; im ...
- java项目连接jdbc报错:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server
java项目连接jdbc报错:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not creat ...
- DirectX 安装报错: 不能信任一个安装所需的压缩文件,请检查加密服务是否启用并且cabinet文件证书是否有效
DirectX 安装报错 不能信任一个安装所需的压缩文件,请检查加密服务是否启用并且cabinet文件证书是否有效 是直播软件open broadcaster software,这个软件安装的时候提示 ...
- Nginx配置SSL报错 nginx: [emerg] unknown directive "ssl"
Nginx配置SSL报错 nginx: [emerg] unknown directive "ssl" 出现如图所示错误,处理办法如下 去nginx解压目录下执行 ./co ...
随机推荐
- quick: setup_mac.sh分析
//quick: setup_mac.sh分析 //quick: setup_mac.sh分析#!/bin/bash //获取并打印根目录QUICK_V3_ROOTDIR="$( cd &q ...
- Tecplot显示周期和对称算例
源视频链接:https://pan.baidu.com/s/1HdU3nsti8qLZhXvISxsSFA 提取码: 3kfu 模型链接:https://pan.baidu.com/s/1CQCGL7 ...
- 在 Mac、Linux、Windows 下Go交叉编译
Golang 支持交叉编译,在一个平台上生成另一个平台的可执行程序,最近使用了一下,非常好用,这里备忘一下. Mac 下编译 Linux 和 Windows 64位可执行程序 CGO_ENABLED= ...
- ubuntu之路——day20 昨天和今天搞定Res18并在GPU上运行 明天YOLO在车辆识别上试一下
- 最长公共子序列长度(dp)
/// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...
- RocketMQ常用命令【转】
首先进入 RocketMQ 工程,进入/RocketMQ/bin 在该目录下有个 mqadmin 脚本 . 查看帮助: 在 mqadmin 下可以查看有哪些命令 a: 查看具体命令的使用 : ...
- Linux下的IO监控与分析(转)
各种IO监视工具在Linux IO 体系结构中的位置 源自 Linux Performance and Tuning Guidelines.pdf 1 系统级IO监控 iostat iostat -x ...
- jsch channel is not opened原因及解决
潜在原因1:服务器sshd限制,可通过如下解决: 潜在原因2:服务器负载过高,超时.在连接处增加超时时间session.setTimeout(60000);默认情况下,socket的超时时间大约为21 ...
- 虚拟机ubuntu16下cheese打开摄像头黑屏问题
在win7上安装了一个ubuntu1604的虚拟机: 在虚拟机下打开电脑上连接的摄像头时,用ubuntu16自带的cheese软件查看是黑屏: 但是cheese上有摄像头名字显示,就是打不开:如下图 ...
- 将innodb置为只读模式
1.关闭change bufferset global.innodb_change_buffering=0; 2.将mysql执行slow shutdown(即干净关闭)set global.inno ...