okhttpclient.setsslsocketfactory 报错解决

以前一直都用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,需要增加这两行。

这样okhttp也就可以下载https的文件了,也可以更新apk文件了。

你可能感兴趣的:(网络)