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 ...
随机推荐
- nginx.conf 配置解析之 http配置
官方文档 http://nginx.org/en/docs/参考链接: https://segmentfault.com/a/1190000012672431参考链接: https://segment ...
- mac下 Eclipse代码自动补齐 设置
Eclipse -> Perferences -> Java -> Editor -> Content Assist如下图. 将auto activation triggers ...
- jmeter(五十一)_性能测试中的服务器资源监控与分析
概述 性能测试过程中,对服务器资源的监控是必不可少的.这里的资源又分了两块,windows和linux linux下监控资源 访问网址http://jmeter-plugins.org/downl ...
- [技术博客]利用第三方框架react-native-swipeout实现左右滑动出现按钮
在之前的开发中,为了实现用户不同手势操作能够对应不同的功能,我们考虑使用React-Native的API--PanResponder,实现识别用户的手势,实现不同的功能.但我们很快就发现,这样简单的实 ...
- gradle/gradle plugin/Android studio关系
gradle - 构建工具,存储于Users/stono/.gradle/wrapper/dists Adroid Studio- IDE Gradle plugin - 在AS中使用Gradle的插 ...
- tornado多进程模式不同进程写不同日志
#coding: utf- ''' Author: Time: Target: ''' import logging import logging.handlers import os import ...
- Attribute application@allowBackup value=(true) from AndroidManifest.xml:7:9-35
1: 在 AndroidManifest.xml 配置文件中显式配置 android:allowBackup=false. 项目中代码 allowBackup="true" 改为 ...
- iOS逆向(五)-ipa包重签名
为什么要重签名? 1.在没有源代码的情况下,你已经对某个应用进行了资源修改(比如修改了启动图或图标等).修改完成以后,如果想要让APP可以正常使用,该APP一定要重新签名然后压缩成IPA文件. 2.如 ...
- javascript常用方法 - String
// 1.长字符串 // 1.1 let longString1 = "This is a very long string which needs " + "to wr ...
- Windows 反调试技术——OpenProcess 权限过滤 - ObRegisterCallback
转载: https://blog.xpnsec.com/anti-debug-openprocess/ 看雪翻译:https://bbs.pediy.com/thread-223857.htm 本周我 ...