2014-05-21 71 views
1

我通過測試框架連接到Github的API,而其直接連接到連接端點之後(在api.github.com任何端點),它會給以下錯誤:SSL錯誤framewrok

https://api.github.com/repos/myrepo/Test/git/blobs/929246f65aab4d636cb229c790f966afc332c124

FAILED: testGetBlobWithMandatoryParameters 
     github {getBlob} integration test with mandatory parameters. 
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. 

如果我使用普通的Java客戶端,此連接正在工作,只有當我通過測試框架進行連接時纔會出現問題。

我的代碼:

@Test(priority = 1, description = "github {getBlob} integration test.") 
    public void testGetBlobWithMandatoryParameters() throws IOException, JSONException{ 
      String urlStr = "https://api.github.com/repos/myrepo/Test/git/blobs/929246f65aab4d636cb229c790f966afc332c124"; 

    URL url = new URL(urlStr); 
    HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); 

    httpConnection.setRequestMethod("GET"); 
    InputStream responseStream = null; 
    String responseString = null; 

    if (httpConnection.getResponseCode() >= 400) { 
     responseStream = httpConnection.getErrorStream(); 
    } else { 
     responseStream = httpConnection.getInputStream(); 
    } 



    if (responseStream != null) { 

     StringBuilder stringBuilder = new StringBuilder(); 
     byte[] bytes = new byte[1024]; 
     int len; 

     while ((len = responseStream.read(bytes)) != -1) { 
      stringBuilder.append(new String(bytes, 0, len)); 

     } 

     if (!stringBuilder.toString().trim().isEmpty()) { 
      responseString = stringBuilder.toString(); 
     }    
    }  
    System.out.println(responseString); 
} 

回答

0

添加

System.setProperty("javax.net.ssl.trustStore", "keystore.jks") 
System.setProperty("javax.net.ssl.trustStorePassword", "changeit")