gpt4 book ai didi

java - 当我向 URL 发布 https 请求时出现 "unable to find valid certification path to requested target"

转载 作者:行者123 更新时间:2023-12-01 15:28:55 29 4
gpt4 key购买 nike

我已在本地 Tomcat 中完成 SSL 配置。
当我调用 getOutputStream() 时抛出异常

public static InputStream send( String uri, Map<String, String> queryString, 
Map<String, String> headers, String method, String reqBody) throws IOException
{
String body = (reqBody != null ? reqBody : "");

//URL myURL = new URL(addUrlParam(uri, queryString));
URL myURL = new URL(uri);
HttpURLConnection httpConn = (HttpURLConnection)myURL.openConnection();

httpConn.setRequestMethod(method);
httpConn.setRequestProperty("Content-Length", String.valueOf(body.toString().getBytes().length));

if ( headers != null ) {
for ( String key : headers.keySet() ) {
httpConn.setRequestProperty(key, headers.get(key));
}
}

httpConn.setDoInput(true);

//POST
if (!HTTP_GET.equals(method) || body.length() > 0) {
httpConn.setDoOutput(true);
httpConn.setUseCaches(false); //POST do not use user caches
***httpConn.getOutputStream().write(body.toString().getBytes());***
httpConn.getOutputStream().flush();
}

return httpConn.getInputStream();
}

如何解决这个问题?

提前致谢!!

最佳答案

Java 需要已知根 CA 的有效证书路径。如果您尝试访问具有自签名证书的站点,则需要将自签名证书的 CA key 作为 CA key 添加到您的 keystore 中。假设您的 CA 证书位于文件 cacert.pem 中,请按如下方式使用 keytool:

keytool -importcert -file cacert.pem -keystore client.jks -storepass some-password

关于java - 当我向 URL 发布 https 请求时出现 "unable to find valid certification path to requested target",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9815989/

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