我正在尝试使用java获取站点的内容。但是得到pxip错误
我正在尝试使用java获取站点的内容。像这样我正在尝试使用java获取站点的内容。但是得到pxip错误,java,ssl,Java,Ssl,我正在尝试使用java获取站点的内容。像这样 package javaTests; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import javax.net.ssl.HttpsURLConnection; public class PrintContent
package javaTests;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class PrintContent {
public static void main(String[] args) throws Exception {
String https_url = "https://api.precharge.net/charge";
URL url;
url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
if(con!=null){
try {
System.out.println("****** Content of the URL ********");
BufferedReader br =
new BufferedReader(
new InputStreamReader(con.getInputStream()));
String input;
while ((input = br.readLine()) != null){
System.out.println(input);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
但我也有例外
javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径生成失败:sun.security.provider.certpath.SunCertPathBuilderException:找不到请求目标的有效证书路径
请就如何解决这个问题给出一些建议。谢谢你
我还尝试将证书复制到java的安全文件夹中。但它不起作用。
还尝试了installcerticate.java类。如果在信任库中安装证书失败,可以使用此技巧禁用SSL验证。不应在生产环境中使用
package javaTests;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
import java.security.SecureRandom;
public class PrintContent {
public static void main(String[] args) throws Exception {
String https_url = "https://api.precharge.net/charge";
URL url;
url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
if (con != null) {
try {
System.out.println("****** Content of the URL ********");
BufferedReader br =
new BufferedReader(
new InputStreamReader(con.getInputStream()));
String input;
while ((input = br.readLine()) != null) {
System.out.println(input);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void disableCertificates() {
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
System.out.println("Warning! SSL validation has been disabled!");
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
System.out.println("Warning! SSL validation has been disabled!");
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
System.out.println("Warning! SSL validation has been disabled!");
}
}
};
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
System.out.println("Failed to disable SSL validation," + e.getMessage());
}
}
}
我不想把它标为复制品,因为我对这个主题了解不多,但你看过:@TomJonckheere。我不知道如何使用它。我把它作为一门独立的课程来写。不在服务器中。不确定此帮助是否正确,链接问题中的链接是否有帮助?您好@raghavendra我执行了相同的程序,但是我没有得到您上面提到的任何异常。我得到了代码的未知主机异常。因此我放置了google.com URL,而不是您的URL。代码运行良好。它为我提供了html内容。@bhadram for google它也在我的系统中运行。如何在我的系统中安装cer证书