繁体   English   中英

使用 jsoup 连接不受信任的证书

[英]Using jsoup to connect to untrusted certificate

我的 Java 应用程序有问题。 我使用 JSOUP 连接到一个以前是 http 而现在移动到 https 的网站。 现在,当我运行该应用程序时,出现以下错误,是否有人能够对此提供帮助?

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
    at java.security.cert.CertPathBuilder.build(Unknown Source)
    ... 32 more
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(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)

如果您信任该站点,则可以通过将其设置为 true 来忽略 http 错误:

Document doc = Jsoup.connect("your_url").ignoreHttpErrors(true).get();

并忽略 TSL 验证,设置 validateTLSCertificates(false):

Document doc = Jsoup.connect("your_url").validateTLSCertificates(false).get();

编辑

此答案已过时,因为 JSoup 已弃用并删除了 1.12.1 版中的 validateTLSCertificates 方法

https://jsoup.org/news/release-1.12.1 )。

如果您信任有问题的站点并想忽略 TLS 验证,请查看此答案how-to-resolve-jsoup-error-unable-to-find-valid-certification-path

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM