彻底解决:SunCertPathBuilderException: unable to find valid certification path to requested target错误的方法
2023-09-11 14:15:13 时间
请求12306系统查票。之前正常的,现在提示这样的错误:
Exception in thread "main" 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
如下图:
导致原因:由于12306安全系统升级不再支持TLS 1.0,所以。以前jsoup直接可以请求的。现在会被拒绝,
解决方法有4种:
1,直接绕过HTTPS验证 TLS2.0+
/**
* 绕过HTTPS验证 TLS2.0+
*/
static public void initTLS() {
try {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
} catch (NoSuchAlgorithmException | KeyManagementException ignored) {
}
}
调用:在请求代码前执行即可。简单方便高效
initTLS();
2:也是绕过HTTPS验证,跟第一种其实差不多。但是调用方法有区别
private static SSLSocketFactory socketFactory() {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
return sslContext.getSocketFactory();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException("Failed to create a SSL socket factory", e);
}
}
调用:在请求代码中执行即可
Connection.Response response = Jsoup.connect(getTicketListUrl)
.headers(header)
.timeout(6000)
.sslSocketFactory(socketFactory())
.method(Connection.Method.GET)
.execute();
注意添加这个:.sslSocketFactory(socketFactory())
3:旧版本的Jsoup有一个参数:据说加上就行了。我现在用的版本没有,估计是取消了
.validateTLSCertificates(false)
4:导入证书:请参考这个连接:
彻底解决unable to find valid certification path to requested target_Gabriel8304的博客-CSDN博客
总结:
第4种:不推荐,为什么?因为我测试了发现还是不行。不知道为什么。而且如果你写爬虫。每一个网站都要操作一次太麻烦了。
运行效果如下图:
成功收到返回数据并解析
相关文章
- Linux启动网卡时出现RTNETLINK answers: File exists错误解决方法
- beeline执行hql过程中出现错误,权限不足
- 【Oracle】ORA-00054 错误解决方法
- 【记录】vue构建项目npm install错误run `npm audit fix` to fix them, or `npm audit` for details
- Openwrt挂载NTFS硬盘提示“只读”错误的解决方法!
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- mysql中Table is read only错误解决方法
- 数据分析人员常犯的五大错误以及预防方法
- 安装DEDECMS出现Function ereg_replace()错误的解决方法
- mysql中You can’t specify target table for update in FROM clause错误解决方法
- 数据分析人员常犯的五大错误以及预防方法
- SAP CRM中间件错误IB_CRM_UPLOAD_MSG的解决方法
- SAP Commerce Cloud OCC API UnknownResourceError 错误
- SAP CRM中间件下载equipment时遇到的一个错误
- Atitit.部分错误 设计模式,异常处理框架atiPartErr 的总结
- 关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论
- 使用brew cask安装minikube的各种错误和解决方法
- 成功解决.append方法出现错误IndexError: list index out of range
- (5/8)枚举的错误用法 之 方法体内部
- Error attempting to get column ‘xxx‘ from result set. Cause: java.sql.SQLDataException错误的解决方法
- nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘xxx‘ not found错误的详细解决方法
- 【错误记录】Android Studio 编译报错 ( Module was compiled with an incompatible version of Kotlin. The binary )
- 段错误?打的就是段错误!!
- LabVIEW的内部错误
- Java中出现“错误: 编码GBK的不可映射字符”的解决方法
- 【XS128】Link error L1822 symbol _FADD / _FSUB/ _FDIV/ _FMUL.....错误解决的方法
- jsp中的http status 500错误问题怎么解决
- 运行bee run之后出现的错误以及解决方法Failed to build the application:
- Mybatis出现Parameter ‘XXX‘ not found. Available parameters are [0, 1, param1, param2]错误解决方法
- LoadRunner 11 监控 LINUX 服务器资源,47190 错误解决方法