gpt4 book ai didi

java - testng框架中的SSL错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:51 25 4
gpt4 key购买 nike

我通过测试框架连接到 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);
}

最佳答案

添加

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

关于java - testng框架中的SSL错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23774773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com