android 访问https 证书问题 报错javax.net.ssl.SSLHandshakeException
2017-08-18 17:36
866 查看
服务器url 切换为https后所有接口调用报错javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
解决办法:
Android项目中使用 retrofit + rxjava +mvvp 架构
故在RetrofitConfig.java 的配置文件中 初始化
方法中加入以下代码
其中 new_dd 文件是在raw目录下
生成.cer文件的方式给出链接自己去看,我就不重复了
http://blog.csdn.net/shenxiandashu/article/details/46456403
解决办法:
Android项目中使用 retrofit + rxjava +mvvp 架构
故在RetrofitConfig.java 的配置文件中 初始化
initOkhttp(){}
方法中加入以下代码
OkHttpClient.Builder builder = new OkHttpClient.Builder(); int[] certificates = {R.raw.new_dd}; String hosts[]= {"*.diandianwaimai.com"}; //builder.sslSocketFactory(SslContextFactory.getSSLSocketFactory(DDApplication.getInstance().getContext(), certificates)); //builder.hostnameVerifier(SslContextFactory.getHostnameVerifier(hosts)); try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null); String certificateAlias = Integer.toString(0); keyStore.setCertificateEntry(certificateAlias, certificateFactory. generateCertificate(DDApplication.getInstance().getContext().getResources().openRawResource(R.raw.new_dd))); SSLContext sslContext = SSLContext.getInstance("TLS"); final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); sslContext.init ( null, trustManagerFactory.getTrustManagers(), new SecureRandom() ); builder.sslSocketFactory(sslContext.getSocketFactory()); builder.hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); } catch (CertificateException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (Exception ex) { }
其中 new_dd 文件是在raw目录下
生成.cer文件的方式给出链接自己去看,我就不重复了
http://blog.csdn.net/shenxiandashu/article/details/46456403
相关文章推荐
- android ksoap2 访问https javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorExce
- andorid HTTPS 不需要证书 VolleyEror: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not fou
- 解决Java调用https服务证书错误javax.net.ssl.SSLHandshakeException
- 使用Java访问https接口javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
- 访问https,抛出的异常javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
- Android 使用 HTTPS 问题解决(SSLHandshakeException)
- 解决Java调用Azure SDK证书错误javax.net.ssl.SSLHandshakeException
- javax.net.ssl.SSLHandshakeException(Cas导入证书)
- 发送请求错误Android:javax.net.ssl.SSLHandshakeException: Handshake failed
- 解决Java调用Azure SDK证书错误javax.net.ssl.SSLHandshakeException
- com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: Handshake failed
- Android ksoap 访问https SSL Webservice报错 javax.net.ssl.SSLException: Not trusted server certificate
- com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertP
- javax.net.ssl.SSLHandshakeException(Cas导入证书)
- 使用https出错javax.net.ssl.SSLHandshakeException: Handshake failed
- 解决 Java 调用 Azure SDK 证书错误 javax.net.ssl.SSLHandshakeException
- 处理Https 异常记录 javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
- xUtils3 Https请求报错:javax.net.ssl.SSLPeerUnverifiedException: Hostname ***.****.**not verified,跳过证书检测
- FireFox火狐浏览器打开证书管理器方法& javax.net.ssl.SSLHandshakeException
- javax.net.ssl.SSLHandshakeException: sun.security.validator 问题解决,与环境有关