【发布时间】:2020-06-29 08:21:45
【问题描述】:
Hy
我尝试将 java 程序连接到 REST API。
使用相同部分的代码,我在 Java 6 中有一个 Java 异常,它在 Java 8 中运行良好。
环境是一样的:
- 信任
- 机器
- unix 用户
代码:
import java.io.DataInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class MainClass {
public static void main (String[] args){
String serviceUrl = "https://api.domain.com" + "/endpont/path";
try {
URL url = new URL(serviceUrl);
URLConnection connection = null;
try{
connection = url.openConnection();
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
String body = "";
String inputLine;
DataInputStream input = new DataInputStream (connection.getInputStream());
while (((inputLine = input.readLine()) != null)){
body += inputLine;
}
System.out.println(body);
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
Java 6 中的错误:sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certificate path to requested target
有人知道它有什么不同吗?我可以使用一些技巧在 Java 6 中获得相同的结果吗?
证书的 CN 是通配符:“*.domain.com”。这可能是原因?
我尝试了几个 API,但都使用了 sun SSL 层。你知道其他人可以代替它吗?
【问题讨论】:
-
点击链接一步一步:mkyong.com/webservices/jax-ws/…
-
不再使用 Java 6 的众多原因之一。 Java 6 的上一次公开发布是在 7 年前。
-
我同意你的观点,但我没有选择项目中的 java 版本。