彻底解决:SunCertPathBuilderException: unable to find valid certification path to requested target错误的方法

这篇具有很好参考价值的文章主要介绍了彻底解决:SunCertPathBuilderException: unable to find valid certification path to requested target错误的方法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

请求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

如下图:

suncertpathbuilderexception: unable to find valid certification path to requ,Java,java,TLS,SSL

 导致原因:由于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种:不推荐,为什么?因为我测试了发现还是不行。不知道为什么。而且如果你写爬虫。每一个网站都要操作一次太麻烦了。

运行效果如下图:

suncertpathbuilderexception: unable to find valid certification path to requ,Java,java,TLS,SSL

suncertpathbuilderexception: unable to find valid certification path to requ,Java,java,TLS,SSL 

成功收到返回数据并解析 文章来源地址https://www.toymoban.com/news/detail-743455.html

到了这里,关于彻底解决:SunCertPathBuilderException: unable to find valid certification path to requested target错误的方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Maven 私服 unable to find valid certification path to requested target 错误

    你遇到的错误信息 “sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target” 表明在 SSL/TLS 握手过程中,证书路径验证失败。这通常是由于缺少或不受信任的证书导致的,Maven 无法与远

    2024年02月08日
    浏览(41)
  • RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

    RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

    使用yolov5l模型训练时出现报错,但是昨天使用yolov5s模型时是可以正常训练的。 发生报错的原因是gpu内存占用过高,terminal输入nvidia-smi查看gpu的使用情况。   我们需要把bach_size调小,一般建议是8的倍数,内存不够用时尽量调低,此处我设置成了16。 结果运行正常。 使用yol

    2024年02月11日
    浏览(38)
  • 【docker】 Unable to find image的解决办法

      今天尝试了下docker,发现存在以下问题,进行记录。 时间:2023-12-26 操作系统:centos opencloudos(腾讯云服务器所用centos) 1、pull测试的hello-world镜像报错: 查了下,需要新建daemon.json文件,把docker国外源变更为国内源。 2、尝试 在里面insert: 然而并不支持,重新运行docker报

    2024年02月19日
    浏览(35)
  • 2023年 Android Studio Unable to find bundled Java version 解决方法

    2023年 Android Studio Unable to find bundled Java version 解决方法

    学习flutter过程中发现 Unable to find bundled Java version 错误 搜索网上的解决方案都不对 最后在 b站 https://www.bilibili.com/video/BV1S4411E7LY?p=17vd_source=d7cf0e2cd70b3cc57314d2efcb598c3d 教程的课件中找到了解决方哦 我的flutter版本 jdk jdk-19.0.2 操作系统 windows11 如果是macOS系统,在jbr同目录下创建一

    2023年04月08日
    浏览(39)
  • frida注入进程报错Failed to spawn: unable to find application with identifier的一种解决思路

    frida注入进程报错Failed to spawn: unable to find application with identifier的一种解决思路

    之前写过一篇文章【转载】绕过ssl pining使用burp抓包微信小程序_HRay\\\'s blog-CSDN博客 今天在测试一款app的时候使用frida-ps -U查看进程结果如下 乐学小鹅为我们要注入的进程,但是注入进程时使用命令 frida -U -f 乐学小鹅 -l D:ADBfridascript.js --no-paus 爆出错误Failed to spawn: unable to fin

    2024年02月11日
    浏览(39)
  • 解决:Unable to add window -- token android.os.BinderProxy is not valid; is your activity running?

    这个问题是我想存储已经创建过的弹窗进行复用过程当中出现的报错,具体代码请看: 在查看报错之后可以确定是 Context 上下文的问题。 在创建Dialog时需要在构造方法当中传递一个Context,如果这个上下文被销毁即这个Activity已经走了OnDestroy方法。那么这个上下文就无效了,

    2024年02月09日
    浏览(12)
  • Unable to find an entry point named ‘OrtGetApiBase‘ in DLL ‘onnxruntime‘解决办法记录

    Unable to find an entry point named ‘OrtGetApiBase‘ in DLL ‘onnxruntime‘解决办法记录

    最近在做深度学习的项目,onnx用c#程序部署。 然后发现在部署/移植模型的时候遇到了一个问题,找了很多资料都没解决 还得亲自动手了 各种测试以后发现是程序包的问题 解决办法是移除掉项目中已经有的microsoft.ml.onnxruntime包,自己重新安装,即可。 步骤1:包名上右击--》

    2024年02月11日
    浏览(46)
  • Dockerfile: unable to find user xxx : no matching entries in passwd file解决方案

    在制作Docker镜像的过程中,完成软件安装等操作后,一般会切换为普通用户启动相应的服务,就需要用到 USER 指令。但有时候这个指令会报错: docker: Error response from daemon: unable to find user : no matching entries in passwd file. 如果遇到这个问题,可以在使用 USER 指令之前,先创建用户

    2024年02月09日
    浏览(17)
  • CMake Error: CMake was unable to find a build program corresponding to “MinGW Makefiles“.解决方案(亲测有效)

    CMake Error: CMake was unable to find a build program corresponding to “MinGW Makefiles“.解决方案(亲测有效)

    CMake编译OpenCV4.6.0过程中一直出错: CMake Error: CMake was unable to find a build program corresponding to “MinGW Makefiles”. CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool. 解决方案如下: 将CMAKE_MAKE_PROGRAM项后面的路径设置为Qt安装路径下的路径。例如:D:/Qt/Qt5.12.12/Tools/mingw7

    2024年02月15日
    浏览(38)
  • Unable to initialize Git; AggregateError(2) Error: Unable to find git Error: Unable to find git

    MacBook Pro m1 升级系统到13版本后,系统中原有的git突然就不能用了,,,输入git -v 既然打印不到版本号???使用which git 是能打印到配置路径的,,, 解决方案: stack overfolw 帖子地址 然后会跳出一个弹窗让你安装xcode-select 点击安装,自动安装完成,控制台再输入git -v就能

    2024年02月12日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包