本文主要是介绍由于手机中的时间不对,导致的https网络连接失败,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在维护别人的代码,发现,如果手机的时间是老的,不正确,https网络访问
onFailure:javax.net.ssl.SSLHandshakeException: com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate: current time
百度搜索到,这个异常说的是在校验证书的时候出现时间校验失败,
另外看了项目,看到okhtttp3 https 访问证书没有设置,根本没有做对HTTPs 的支持操作。最后自己加了信任所有证书的操作
private static OkHttpClient.Builder setSSL(OkHttpClient.Builder builder) {try {// Create a trust manager that does not validate certificate chainsfinal TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {@Overridepublic void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {}@Overridepublic void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {}@Overridepublic java.security.cert.X509Certificate[] getAcceptedIssuers() {return new java.security.cert.X509Certificate[]{};}}};// Install the all-trusting trust managerfinal SSLContext sslContext = SSLContext.getInstance("SSL");sslContext.init(null, trustAllCerts, new java.security.SecureRandom());// Create an ssl socket factory with our all-trusting managerfinal javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();builder.sslSocketFactory(sslSocketFactory);builder.hostnameVerifier(new HostnameVerifier() {@Overridepublic boolean verify(String hostname, SSLSession session) {return true;}});return builder;} catch (Exception e) {throw new RuntimeException(e);}}
这篇关于由于手机中的时间不对,导致的https网络连接失败的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!