以前一直都用HttpUrlConnection这个类来做通信和进行文件下载,apk更新升级等。
后续因为更新apk的时候包体积较大,网络不好的时候,用HttpUrlConnection偶尔会下载失败,于是下载更新apk就改为了okhtt3这个库,其他业务请求还是用HttpUrlConnection这个类。
至于用什么版本的okhttp3进行通信其实请求代码差别不大,比如我用的是
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
后续因为业务改动,服务端改为了https,所有的网络请求都用https方式了,
HttpUrlConnection改为https也是需要提供
SSLSocketFactory 和 X509TrustManager ,代码见下:
try { TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) { } }}; SSLContext sc = SSLContext.getInstance("TLS"); //trustAllCerts信任所有的证书 sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { //boolean verify = HttpsURLConnection.getDefaultHostnameVerifier().verify("你的域名", session); //return verify; return true; } }); } catch (Exception e) { e.printStackTrace(); }
给HttpUrlConnection设置SSLSocketFactory和HostnameVerifier即可进行https通信。
这样修改后,HttpUrlConnection就可以请求https了,当然这里是信任所有https,实际上如果要真的验证还是需要拿到证书文件,进行实际验证的,此处略,只是暂时为了让通信能成功。
那么okhttp3的修改也是类似的,需要提供SSLSocketFactory和TrustManager,就拿上面的okhttp3的3.10.0版本进行改动,见下:
private SSLSocketFactory createSSLSocketFactory() { SSLSocketFactory ssfFactory = null; try { MyTrustManager mMyTrustManager = new MyTrustManager(); SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, new TrustManager[]{mMyTrustManager}, new SecureRandom()); ssfFactory = sc.getSocketFactory(); } catch (Exception e) { e.printStackTrace(); } return ssfFactory; } //实现X509TrustManager接口 private static class MyTrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } } private static class TrustAllHostnameVerifier implements HostnameVerifier { public boolean verify(String hostname, SSLSession session) { return true; } }
在OkHttpClient请求的时候,增加两行代码即可:
OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .sslSocketFactory(createSSLSocketFactory(), new MyTrustManager()) .hostnameVerifier(new TrustAllHostnameVerifier()) .build();
然后用OkHttpClient去请求即可。
其中
.sslSocketFactory(createSSLSocketFactory(), new MyTrustManager()) .hostnameVerifier(new TrustAllHostnameVerifier())
这两行是这里增加的,以前没有,现在为了支持https,需要增加这两行。文章来源:https://www.toymoban.com/news/detail-813479.html
这样okhttp也就可以下载https的文件了,也可以更新apk文件了。文章来源地址https://www.toymoban.com/news/detail-813479.html
到了这里,关于okhttpclient.setsslsocketfactory 报错解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!