javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building
2016-11-16 10:52
771 查看
昨天接到领导布置的任务,与集团旗下一个子公司妙健康做接口开发,他们的接口是https的,因此我采用HttpsURLConnection来建立https网站发起的请求连接,测试demo代码如下:
package com.pcmall;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.*;
public class TrustSSL {
public static void main(String[] args) throws Exception {
URL console = new URL("https://xxx.xxx.xxx/xxx/xxx/xxx");
HttpURLConnection conn = (HttpURLConnection) console.openConnection();
conn.connect();
System.out.println(conn.getResponseCode());
}
}报如下错误:
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
网上百度了一下,说是HTTPS证书过时导致的,我们在这里写一个假的安全验证,则成功解决该问题,解决成功后代码如下:
package com.pcmall;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.*;
public class TrustSSL {
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
public static void main(String[] args) throws Exception {
URL console = new URL("https://xxx.xxx.xxx/xxx/xxx/xxx");
HttpURLConnection conn = (HttpURLConnection) console.openConnection();
if (conn instanceof HttpsURLConnection) {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
((HttpsURLConnection) conn).setSSLSocketFactory(sc.getSocketFactory());
((HttpsURLConnection) conn).setHostnameVerifier(new TrustAnyHostnameVerifier());
}
conn.connect();
System.out.println(conn.getResponseCode());
}
}这个方法问了一圈同事,居然没人知道,我很是无语。
package com.pcmall;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.*;
public class TrustSSL {
public static void main(String[] args) throws Exception {
URL console = new URL("https://xxx.xxx.xxx/xxx/xxx/xxx");
HttpURLConnection conn = (HttpURLConnection) console.openConnection();
conn.connect();
System.out.println(conn.getResponseCode());
}
}报如下错误:
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
网上百度了一下,说是HTTPS证书过时导致的,我们在这里写一个假的安全验证,则成功解决该问题,解决成功后代码如下:
package com.pcmall;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.*;
public class TrustSSL {
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
public static void main(String[] args) throws Exception {
URL console = new URL("https://xxx.xxx.xxx/xxx/xxx/xxx");
HttpURLConnection conn = (HttpURLConnection) console.openConnection();
if (conn instanceof HttpsURLConnection) {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
((HttpsURLConnection) conn).setSSLSocketFactory(sc.getSocketFactory());
((HttpsURLConnection) conn).setHostnameVerifier(new TrustAnyHostnameVerifier());
}
conn.connect();
System.out.println(conn.getResponseCode());
}
}这个方法问了一圈同事,居然没人知道,我很是无语。
相关文章推荐
- javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building
- javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed
- javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f
- javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException: PKIX path building失败
- javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f
- 调用https接口抛出javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path
- Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path
- 解决 javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path buildin
- android ksoap2 访问https javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorExce
- 爬数据出现错误javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException
- 【转载】javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted c
- HttpClient_javax.net.ssl.SSLHandshakeException: sun.security.validator 问题解决,与环境有关
- javax.net.ssl.SSLHandshakeException: sun.security.validator 问题解决,与环境有关
- javax.net.ssl.SSLHandshakeException:PKIX path building failed解决方法
- andorid HTTPS 不需要证书 VolleyEror: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not fou
- javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException:
- javax.net.ssl.SSLHandshakeException:PKIX path building failed多个证书解决方法
- javax.net.ssl.SSLHandshakeException:PKIX path building failed解决方法的代码
- SSLHandshakeException: *.ValidatorException: PKIX path building failed: *.SunCertPathBuilderExceptio
- Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provid