我有一个服务器和域 nodeserver.fvds.ru在其上配置“让加密”https。 Nginx 将所有请求重定向到监听某个端口的本地 java 程序。我的浏览器可以发送请求。我还可以使用名为 Advanced REST Client 的 Chrome 插件发送请求。但是如果我尝试像这样使用 Java 发送获取请求:
public static String getResponse(String urlToRead) throws Exception {
StringBuilder result = new StringBuilder();
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
public static void main(String[] args) throws Exception
{
System.out.println(getResponse("https://nodeserver.fvds.ru/"));
}
我会得到
Exception in thread "main" 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 将无法找到从您的服务器证书到任何受信任的 CA 的证书路径。
如果这只是为了测试目的,您可以将自签名服务器证书添加到您的 Java 客户端信任库(默认情况下,JRE_PATH/lib/security/cacerts)。
如果那是你的生产环境,你应该避免使用自签名证书
关于java - Java 和 HTTPS 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36450606/