即使在禁用TrustManager后我也遇到错误
javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
以下是我用于消费者 Web 服务的代码。
public class HttpsDisable {
public static void disableCertificateValidation() {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
}
};
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//Now you can access an https URL without having the certificate in the truststore
try {
disableCertificateValidation();
URL url = new URL("https://162.19.122.114/service.asmx?wsdl");
System.out.println("url ================ "+url);
String sWebserviceurl = "https://172.17.134.214/service.asmx?wsdl";
String sXMLInput = "<KeedAn><PrAge>26</PrAge></KeedAn>";
AnalysisServiceService sl = new AnalysisServiceServiceLocator();
AnalysisService service = sl.getAnalysisService(new URL(sWebserviceurl));
String in = service.getNeedProdRes(sXMLInput);
System.out.println("web service output ----------\n" + in);
} catch (MalformedURLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
你能告诉我我哪里出错了,因为上面的代码不起作用并抛出异常。而且我还将 .cert 文件保存在 jre/lib/.cert 路径中。