我正在尝试用Java下载https网址的内容,但是什么时候才会收到此错误。
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
我能够与其他安全服务建立https连接,但不是我现在想要的那个。服务器是我们内部的,并有Godaddy证书。这可能是问题吗?
public class DataRegressionTest {
private static String urlString = "https://somedomain.com/GetData";
public static void main(String[] args) throws Exception {
URL url = new URL(urlString);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
String urlContent = StreamUtils.urlConnectionToUrlContent(httpsURLConnection);
JsonNode jsonNode = JsonUtils.jsonToTree(urlContent);
System.out.println(jsonNode);
}
}
我该如何解决这个问题?
这是一个更完整的堆栈跟踪
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(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1884)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1300)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
答案 0 :(得分:1)
如果证书无效,则会发生此异常。如果您使用浏览器打开URL(Mozilla Firefox最适合此测试,IE和Chrome使用Windows域中Active Directory服务器的证书),证书显示为有效吗?
如果不是(我猜)你必须下载证书并将其添加到JVM。