Android 4 +https(如何启动TLS1 1 and TLS1 2)
-
之前用的網(wǎng)絡(luò)請求框架是鴻洋的OkHttpUtils,網(wǎng)絡(luò)請求在5.0手機上使用https沒有問題,但是最近突然使用了一個4.4的系統(tǒng),就報錯SSLException ....咋地咋地
-
然后 我們的副總給我們找一個博客,如何解決4.+系統(tǒng)出現(xiàn)的這個問題(雖然我現(xiàn)在還很有點懵逼...),然后添加上去后,就可以使用了。在Okgo ,OkHttpUtils 都可以用,那么的網(wǎng)絡(luò)請求應(yīng)該也可以用?!!(別的沒有測) -然后我先貼一下那個仁兄的博客吧 鏈接地址 ,使用TLSSocketFactory 類
-
然后就是在全局初始化的時候使用這個類 , 那個第二次參數(shù),就用他提供的。 ··· 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也一樣的,第二個參數(shù)如果不想用框架自帶的我在下面貼出來。
-
這是 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()); } 復(fù)制代碼}
···
總結(jié)
以上是生活随笔為你收集整理的Android 4 +https(如何启动TLS1 1 and TLS1 2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java编程思想 学习笔记1
- 下一篇: android sina oauth2.