andorid HTTPS 不需要证书 VolleyEror: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeE

Posted 景树园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了andorid HTTPS 不需要证书 VolleyEror: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeE相关的知识,希望对你有一定的参考价值。

1.加证书(这里不说)

2.修改代码

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
*
*
* Created by Administrator on 2016/2/17.
*/
public class FakeX509TrustManager 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.
   }

   public boolean isClientTrusted(X509Certificate[] chain) {
       return true;
   }

   public boolean isServerTrusted(X509Certificate[] chain) {
       return true;
   }

   @Override
   public X509Certificate[] getAcceptedIssuers() {
       return _AcceptedIssuers;
   }

   public static void allowAllSSL() {
       HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

           @Override
           public boolean verify(String arg0, SSLSession arg1) {
               // TODO Auto-generated method stub
               return true;
           }

       });

       SSLContext context = null;
       if (trustManagers == null) {
           trustManagers = new TrustManager[] { new FakeX509TrustManager() };
       }

       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());
   }

}

 

在调用网络前使用

FakeX509TrustManager.allowAllSSL(); //it is dangerous!但是有的时候我们需要这样做!!       //========================StringRequest=====================================================       StringRequest httpRequest = new StringRequest(requestMethod, url, new Response.Listener<String>() {           @Override           public void onResponse(String response) { 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

以上是关于andorid HTTPS 不需要证书 VolleyEror: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeE的主要内容,如果未能解决你的问题,请参考以下文章

Andorid进行签名的方式

自签名的https证书是不安全的

andorid jar/库源码解析之frida体验

sh 笔记:OpenSSL生成「自签名」证书,配置Nodejs本地HTTPS服务 - 12.不需要单独创建文件,重新生成证书的方法

哪些网站需要HTTPS(SSL证书)

我的网站https过期了,老是提示不安全怎办 ?