android volley连接https

tcwi4767 8年前

来自: http://my.oschina.net/zengliubao/blog/616666


Android中使用volley进行Https 通讯的时候,如果没有申请正式会报错:( 我们的服务器用nginx作为容器 )

VolleyEror: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

最好的办法是按照规则来办事:加证书。然而调试服务器说不加...

那么要怎么才不会报错呢?

1.查看接口 X509TrustManger.java ( 在包javax.net.ssl )

X509TrustManager.Java  //------------------------------------    package javax.net.ssl;    import java.security.cert.CertificateException;  import java.security.cert.X509Certificate;    /**   * The trust manager for X509 certificates to be used to perform authentication   * for secure sockets.   */  public interface X509TrustManager extends TrustManager {        /**       * Checks whether the specified certificate chain (partial or complete) can       * be validated and is trusted for client authentication for the specified       * authentication type.       *       * @param chain       *            the certificate chain to validate.       * @param authType       *            the authentication type used.       * @throws CertificateException       *             if the certificate chain can't be validated or isn't trusted.       * @throws IllegalArgumentException       *             if the specified certificate chain is empty or {@code null},       *             or if the specified authentication type is {@code null} or an       *             empty string.       */      public void checkClientTrusted(X509Certificate[] chain, String authType)              throws CertificateException;          /**       * Checks whether the specified certificate chain (partial or complete) can       * be validated and is trusted for server authentication for the specified       * key exchange algorithm.       *       * @param chain       *            the certificate chain to validate.       * @param authType       *            the key exchange algorithm name.       * @throws CertificateException       *             if the certificate chain can't be validated or isn't trusted.       * @throws IllegalArgumentException       *             if the specified certificate chain is empty or {@code null},       *             or if the specified authentication type is {@code null} or an       *             empty string.       */      public void checkServerTrusted(X509Certificate[] chain, String authType)              throws CertificateException;        /**       * Returns the list of certificate issuer authorities which are trusted for       * authentication of peers.       *       * @return the list of certificate issuer authorities which are trusted for       *         authentication of peers.       */      public X509Certificate[] getAcceptedIssuers();  }      //-------------------------------------------------------------------------------

 

2.FakeX509TrustManger  implements X509TrustManager

package com.http.utils;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());    }}

 

3.在请求前设置忽略所有的验证,允许所有的SSL

 

(.equals(requestUrl.getMethod())){           requestMethod=Request.Method.;        }{           url=url++params.toString();         }          FakeX509TrustManager.();StringRequest httpRequest = StringRequest(requestMethod, url, Response.Listener<String>() {              onResponse(String response) {  .dismissDialog();