[英]Error when connecting to URL - PKIX path building failed
我正在嘗試連接到網頁以收集信息,我正在使用jsoup來解析HTML。 但是,每當我嘗試連接到URL以下載源時,我都會收到有關PKIX構建路徑的錯誤。 我環顧四周,我發現的所有內容都說將我的網站的CA根證書添加到我的信任庫,但問題仍然存在(CA根證書已經存在)。 我可以通過Web瀏覽器連接到網站,但不能通過URL類連接到網站。 這是我能編寫的最基本的代碼,它會產生錯誤。
public class URLConnectStart {
public static void main(String[] args) {
try {
URL u = new URL("https://ntst.umd.edu/soc/");
u.openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這是錯誤
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.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at URLConnectStart.run(URLConnectStart.java:14)
at URLConnectStart.main(URLConnectStart.java:8)
Caused by: 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.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 16 more
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)
... 22 more
有關網站證書的Chrome信息
任何幫助,將不勝感激。 這不是一個關鍵的應用程序,因此安全性並不是那么重要,但如果我能保持安全性,我寧願這樣做。 無論如何,我想要做的就是通過代碼下載本網站的HTML。
謝謝。
該網站不提供完成證書鏈所需的中間證書 。 一些用戶代理/瀏覽器具有稱為AIA的功能,他們在那里下載必要的中間件,但Java客戶端不是其中之一。
如果您是站點管理員,解決此問題的正確方法是提供中間證書,以便發送完整的鏈。 即使您是最終用戶,也請考慮與網站聯系以解決此問題。 由於此問題,使用Android瀏覽器的人也將無法訪問此站點而未接受安全警告。
與此同時,如果您想在客戶端中解決此問題,可以下載缺少的中間證書並將其添加到Java證書庫中 。
您可以手動將證書安裝到Java密鑰庫。
用於清除是否存在任何別名的第一個命令(我更喜歡在Windows中從JAVA_HOME運行此命令,因此路徑與此相關。如果您從外面運行,請相應地更改路徑。
現在我們需要安裝證書
密鑰庫的默認密碼是changeit
如果您有多個證書,請將此命令添加到每個文本文件中,並將其保存為bat擴展名並放入JAVA_HOME \\ bin或任何您喜歡的位置。 確保提到的路徑是相對的。
如果你的jre在jdk之外,那么提供它的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.