Kosp-SSL相關

 //專案

package com.example.danny.toxicgpsauditapp;


//以下是新增class


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 DannyDev on 2017/10/18.

*/



public class SsX509TrustManager 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;

   }


   /**

    * 允许所有的SSL请求,添加在new StringRequest()之前

    */

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

       }


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

   }


}



//使用範例

public static String GetWebService(String nameSpace, String methodName, String parameter,

                                  String url, String action) {


   SoapObject soapObj = new SoapObject(nameSpace, methodName);

   soapObj.addProperty(parameter, action);

   //soapObj.addProperty(parameter2, action2);

   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);


   envelope.bodyOut = soapObj;

   envelope.dotNet = true;

SsX509TrustManager.allowAllSSL(); //加入這句



   HttpTransportSE transport = new HttpTransportSE(url);


   try {

       /**

                    * 連接網絡獲取數據

                    */

       transport.call(nameSpace + methodName, envelope);

       SoapObject soapReault = (SoapObject) envelope.bodyIn;

       String result = soapReault.getProperty(0).toString();

       return result;


   } catch (SoapFault e) {


       return e.getMessage();


   } catch (ConnectException e) {


       /**

                    * 無網絡連接

                    */

       return "ConnectException";


   } catch (IOException e) {


       return e.getMessage();


   } catch (XmlPullParserException e) {


       return e.getMessage();


   } catch (Exception e) {


       return e.getMessage();


   }

}


留言

這個網誌中的熱門文章

[轉貼] 如何正確設定 AFNetworking 的安全連線

Xcode iOS 打電話功能