发布网友 发布时间:2022-04-20 08:00
共1个回答
热心网友 时间:2023-09-01 22:08
因为最近公司的open api服务器访问协议换成了https,所以 android 在使用okhttp 走https 访问的时候遇到了证书信任的问题,
在这里把我走过的弯路记下来,一如既往的话不多说,上码:
OkHttpClient sClient = new OkHttpClient();
// 设置超时时间
sClient.setConnectTimeout(8000, TimeUnit.MILLISECONDS);
sClient.setReadTimeout(8000, TimeUnit.MILLISECONDS);
// 注册*
sClient.interceptors().add(new BaseInterceptor(context));
第一种方式:
sClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
运行结果:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
11-26 11:17:57.2 17106-17268/com.dooioo.addressbook W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
11-26 11:17:57.2 17106-17268/com.dooioo.addressbook W/System.err: at com.squareup.okhttp.Connection.connectTls(Connection.java:235)
11-26 11:17:57.2 17106-17268/com.dooioo.addressbook W/System.err: at com.squareup.okhttp.Connection.connectSocket(Connection.java:199)
11-26 11:17:57.2 17106-1726