2017-06-22 42 views
0

我從https://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/ 繼mkyong教程,但不是調用本地主機,我使用這個網站:https://httpbin.org/get錯誤在一個簡單的HTTPS GET測試

我能夠使之與HTTP工作,但不與HTTPS。當我嘗試使用HTTPS運行它,我得到這個錯誤:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: 
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderExce 
ption: 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 S 
ource) 
     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown So 
urce) 
     at java.net.HttpURLConnection.getResponseCode(Unknown Source) 
     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unk 
nown Source) 
     at com.my.app.HTTPSGetTest.main(HTTPSGetTest.java:22) 
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: 
sun.security.provider.certpath.SunCertPathBuilderException: unable to find vali 
d 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 Sour 
ce) 
     ... 15 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 Sourc 
e) 
     at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown 
Source) 
     at java.security.cert.CertPathBuilder.build(Unknown Source) 
     ... 21 more 

這裏是我使用的代碼:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.net.ssl.HttpsURLConnection; 

public class HTTPSGetTest { 

    public static void main(String[] args) { 

    try { 

     URL url = new URL("https://httpbin.org/get"); 
     HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
     conn.setRequestMethod("GET"); 
     conn.setRequestProperty("Accept", "application/json"); 

     if (conn.getResponseCode() != 200) { 
     throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); 
     } 

     BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); 

     String output; 
     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 
     System.out.println(output); 
     } 

     conn.disconnect(); 

    } 
    catch (MalformedURLException e) { 

     e.printStackTrace(); 

    } 
    catch (IOException e) { 

     e.printStackTrace(); 

    } 

    } 
} 

我試圖運行InstallCert.java喜歡這裏描述:http://www.mkyong.com/webservices/jax-ws/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/,但我得到以下錯誤這樣做:

Loading KeyStore C:\Program Files\Java\jre1.8.0_31\lib\security\cacerts... 
Opening connection to localhost:3128... 
Starting SSL handshake... 
Exception in thread "main" java.net.SocketException: Connection reset 
     at java.net.SocketInputStream.read(Unknown Source) 
     at java.net.SocketInputStream.read(Unknown Source) 
     at sun.security.ssl.InputRecord.readFully(Unknown Source) 
     at sun.security.ssl.InputRecord.read(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 InstallCert.main(InstallCert.java:105) 

我背後的代理,我用cntlm來訪問代理外的東西。我不知道這是問題還是其他問題,因爲我可以在沒有使用HTTP的問題的情況下執行請求。

回答

相關問題