android volley https

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


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

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。


参考链接:http://www.trinea.cn/android/android-java-https-ssl-exception-2/

转载于:https://my.oschina.net/zengliubao/blog/616666

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值