testng框架中的SSL错误

时间:2014-05-21 05:36:09

标签: java ssl testng github-api

我通过测试框架连接到Github API,当它连接以下端点(api.github.com中的任何端点)时,它会发出以下错误:

  

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);
}

1 个答案:

答案 0 :(得分:0)

添加

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