异常描述:
D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLHandshakeException: Chain validation failed
com.bfmd.okhttpsample I/Main: error: Chain validation failed
异常解决:
解决方法一:
解决方法很简单,检查一下设备系统时间是否在证书有效期内,若没有就改到有效期即可(一般系统时间调整为跟网络一致)。
解决方法二:跳过https验证机制
缺点是信任任何网址,无论是否有证书或者在证书有效期内,可以按需修改为信任指定网址
//自定义SS验证相关类private static class TrustAllCerts implements X509TrustManager {@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}}private static class TrustAllHostnameVerifier implements HostnameVerifier {@Overridepublic boolean verify(String hostname, SSLSession session) {return true;}}private static SSLSocketFactory createSSLSocketFactory() {SSLSocketFactory ssfFactory = null;try {SSLContext sc = SSLContext.getInstance("TLS");sc.init(null, new TrustManager[]{new TrustAllCerts()}, new SecureRandom());ssfFactory = sc.getSocketFactory();} catch (Exception e) {}return ssfFactory;}
设置属性
okBuilder.sslSocketFactory(createSSLSocketFactory());okBuilder.hostnameVerifier(new TrustAllHostnameVerifier());return okBuilder.build();