-
之前用的網絡請求框架是鴻洋的OkHttpUtils,網絡請求在5.0手機上使用https沒有問題,但是最近突然使用了一個4.4的系統,就報錯SSLException ....咋地咋地
-
然后 我們的副總給我們找一個博客,如何解決4.+系統出現的這個問題(雖然我現在還很有點懵逼...),然后添加上去后,就可以使用了。在Okgo ,OkHttpUtils 都可以用,那么的網絡請求應該也可以用?!!(別的沒有測) -然后我先貼一下那個仁兄的博客吧 鏈接地址 ,使用TLSSocketFactory 類
-
然后就是在全局初始化的時候使用這個類 , 那個第二次參數,就用他提供的。 ··· OkHttpClient okHttpClient = null; try { HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null); okHttpClient = new OkHttpClient.Builder() .addInterceptor(new LoggerInterceptor("history")) .sslSocketFactory(new TLSSocketFactory() , sslParams.trustManager)//這里使用!!! .build(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } OkHttpUtils.initClient(okHttpClient); ···
-
用OKgo也一樣的,第二個參數如果不想用框架自帶的我在下面貼出來。
-
這是 TSsX509TrustManager ···
public class SsX509TrustManager implements X509TrustManager { private static TrustManager[] trustManagers; private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {//To change body of implemented methods use File | Settings | File Templates.
}@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {//To change body of implemented methods use File | Settings | File Templates.
}@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {return new java.security.cert.X509Certificate[0];
}public boolean isClientTrusted(X509Certificate[] chain) {return true;
}public boolean isServerTrusted(X509Certificate[] chain) {return true;
}public static X509Certificate[] get_AcceptedIssuers() {return _AcceptedIssuers;
}/*** 允許所有的SSL請求,添加在new StringRequest()之前*/
public static void allowAllSSL() {HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {@Overridepublic boolean verify(String arg0, SSLSession arg1) {// TODO Auto-generated method stubreturn true;}});SSLContext context = null;if (trustManagers == null) {trustManagers = new TrustManager[]{new SsX509TrustManager()};}try {context = SSLContext.getInstance("TLS");context.init(null, trustManagers, new SecureRandom());} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (KeyManagementException e) {e.printStackTrace();}HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}
復制代碼
}
···