异常情况1:
报错拋异常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
这个情况是由于证书为不是JDK官方信任的证书导致的,解决办法有两种,一种升级代码把校验证书信任忽略,一种是需要访问的服务器本地JDK导入证书。
方案一:
新增类实现X509TrustManager,X509TrustManager这个就是校验信任证书的类,把这个类set到httpsConnection里。
private static class TrustAllManager implements X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
}
将AnyHostnameVerifier这个类set进httpsConnection,这个是默认不校验hostname,固定返回true。