2017-07-04 48 views
0
public class URLConnectionReader { 
     public static void main(String[] args) throws Exception { 
      URL urlObj = new URL("https://www.paypal.com"); // enter url here 
      HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.connect(); 
      int code = connection.getResponseCode(); 
      System.out.println(code); 
     } 
} 

我正在創建一個URL測試器,它檢查URL是否正常工作。上面是我的代碼。當我用https://www.paypal.com測試它時,它返回200,當我用https://www.facebook.com測試它時,它會拋出異常。 下面是異常堆棧跟蹤Java URL連接https URL上的奇怪行爲

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 
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) 
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949) 
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) 
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296) 
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509) 
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216) 
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979) 
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914) 
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062) 
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387) 
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153) 
    at URLConnectionReader.main(URLConnectionReader.java:27) 
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(PKIXValidator.java:387) 
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292) 
    at sun.security.validator.Validator.validate(Validator.java:260) 
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324) 
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229) 
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124) 
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491) 
    ... 11 more 
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141) 
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126) 
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) 

at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382) 
... 17 more 

有誰能夠解釋一下我爲什麼https://www.paypal.com返回200,但https://www.facebook.com拋出異常。

回答

0

這裏的問題在於,默認情況下,Java帶有根CA證書的非常有限的集合 。它「接受」比典型的 瀏覽器少得多的CA。解決此問題的最簡單方法是從Chrome或Firefox等瀏覽器中導出一組 CA證書,並使用keytool將它們導入到Java的密鑰存儲庫中 。

ref

+0

不要發佈基本上是來自另一個問題的引用的答案,而是投票或標記爲重複關閉。 –

+0

我看不到標誌中的重複標誌 –

+1

我回滾了您的最後一次編輯,因爲答案中的文本不是您的,因此必須引用和歸因。您的修改刪除了這是一個引用的事實,並刪除了歸屬地,這會使您的回答違反規則(特別是plagiary)。標記爲重複可能需要稍高的聲譽,不確定。 –

0

由於貝寶,是由一個機構是在您的信任簽名的證書,而Facebook並沒有。