Okhttp3信任所有证书设置
2017-07-28 12:01
344 查看
/** * 默认信任所有的证书 * TODO 最好加上证书认证,主流App都有自己的证书 * * @return */ @SuppressLint("TrulyRandom") private static SSLSocketFactory createSSLSocketFactory() { SSLSocketFactory sSLSocketFactory = null; try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, new TrustManager[]{new TrustAllManager()}, new SecureRandom()); sSLSocketFactory = sc.getSocketFactory(); } catch (Exception e) { } return sSLSocketFactory; } private static class TrustAllManager 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 { @Override public boolean verify(String hostname, SSLSession session) { return true; } }
OkHttp配置
//log拦截器 打印所有的log HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { @Override public void log(String message) { LogUtils.e("HttpLog",message); } }); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); //初始化OkHttp OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(NetworkConfig.ConnectionTime, TimeUnit.SECONDS) .addInterceptor(new RetryIntercepter(3)) //添加自定义拦截 .addInterceptor(loggingInterceptor) .sslSocketFactory(createSSLSocketFactory()) .hostnameVerifier(new TrustAllHostnameVerifier()) .build(); //初始化 retrofit Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client(client) .baseUrl(baseUrl) .build();
相关文章推荐
- OkHttp3.1以上信任所有证书
- Okhttp 信任所有证书(自签名证书/抓包fiddler,charles)
- okHttp访问Https-----信任当前证书和所有证书
- OkHttp使用Https访问服务器时信任所有证书
- HttpClient之配置ssl,采用设置信任自签名证书实现https
- Android网络编程——https 不验证证书方式(信任所有证书)
- HttpsURLConnection访问HTTPS链接时信任所有证书
- Android平台实现https信任所有证书的方法
- IIS6的SSL配置,如何配置SSL到登陆页,如何将SSL证书设置成受信任的证书
- java https client信任所有证书
- 设置WebView接受所有网站的证书加载https
- Android下OkHttp请求自定义HTTPS证书接口设置
- 不能修改“System Roots”钥匙串 要更改根证书是否会被信任,请在“钥匙串访问”中打开它,然后修改它的信任设置
- Android开发之信任所有https证书
- 让tomcat信任所有证书并传递给应用程序处理
- 轻松把玩HttpClient之配置ssl,采用设置信任自签名证书实现https
- 不能修改“System Roots”钥匙串 要更改根证书是否会被信任,请在“钥匙串访问”中打开它,然后修改它的信任设置
- Android平台实现https信任所有证书的方法
- Android网络编程——https 不验证证书方式(信任所有证书)
- java https信任所有证书