IOException:javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found
解决方法:
private static SSLSocketFactory createSSLSocketFactory (){
SSLSocketFactory sSLSocketFactory = null ;
try {
SSLContext sc = SSLContext.getInstance("TLS" );
sc.init(null , new TrustManager[]{new TrustAllManager()},
new SecureRandom());
sSLSocketFactory = sc.getSocketFactory();
} catch (Exception e) {
}
return sSLSocketFactory;
}
private static class TrustAllManager implements X509TrustManager {
@Override
public void checkClientTrusted (X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted (X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers () {
return new X509Certificate[0 ];
}
}
private static class TrustAllHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify (String s, SSLSession sslSession) {
return true ;
}
}
mOkHttpClient = new OkHttpClient.Builder()
.sslSocketFactory(createSSLSocketFactory())
.hostnameVerifier(new TrustAllHostnameVerifier())
.build();
参考: http://www.cnblogs.com/alisecurity/p/5939336.html