海
我尝试将 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:无法找到请求目标的有效证书路径
有人知道它有什么不同吗?我可以使用一些技巧在 Java 6 中获得相同的结果吗?
证书的 CN 是通配符:“*.domain.com”。这可能是原因吗?
我尝试了几个 api,但都使用了 sun SSL 层。你知道另一个来代替它吗?