天天看點

HttpsUtil

package com.raiyi.flowcenter.utils.http;





 import java.io.BufferedReader;

 import java.io.InputStream;

 import java.io.InputStreamReader;

 import java.io.OutputStream;

 import java.io.UnsupportedEncodingException;

 import java.net.URL;

 import java.net.URLEncoder;

 import java.security.cert.CertificateException;

 import java.security.cert.X509Certificate;

 import java.util.Map;

 import java.util.Map.Entry;



 import javax.net.ssl.HttpsURLConnection;

 import javax.net.ssl.SSLContext;

 import javax.net.ssl.SSLSocketFactory;

 import javax.net.ssl.TrustManager;

 import javax.net.ssl.X509TrustManager;



 import org.apache.commons.lang.StringUtils;



 /**

  * https 請求 微信為https的請求

  * @author stone

  *

  */

 public class HttpsUtil {

     

     /**

      * 預設編碼

      */

     private static final String DEFAULT_CHARSET = "UTF-8";



     /**

      * HTTPS 的get 請求

      * @param url

      * @return

      */

     public static String get(String url) {

         StringBuffer bufferRes = null;

         try {

             TrustManager[] tm = { new MyX509TrustManager() };  

             SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");  

             sslContext.init(null, tm, new java.security.SecureRandom());  

             // 從上述SSLContext對象中得到SSLSocketFactory對象  

             SSLSocketFactory ssf = sslContext.getSocketFactory();

             

             URL urlGet = new URL(url);

             HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();

             // 連接配接逾時

             http.setConnectTimeout(25000);

             // 讀取逾時 --伺服器響應比較慢,增大時間

             http.setReadTimeout(25000);

             http.setRequestMethod("GET");

             http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

             http.setSSLSocketFactory(ssf);

             http.setDoOutput(true);

             http.setDoInput(true);

             http.connect();

             

             InputStream in = http.getInputStream();

             BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));

             String valueString = null;

             bufferRes = new StringBuffer();

             while ((valueString = read.readLine()) != null){

                 bufferRes.append(valueString);

             }

             in.close();

             if (http != null) {

                 // 關閉連接配接

                 http.disconnect();

             }

             return bufferRes.toString();

         } catch (Exception e) {

             e.printStackTrace();

             return null;

         }

     }

     

     /**

      * get請求https

      * @param url

      * @param params

      * @return

      */

     public static String get(String url, Map<String, String> params) {

         return get(initParams(url, params));

     }

     

     /**

      * HTTPS 的POST 請求

      * @param url

      * @param params

      * @return

      */

     public static String post(String url, String params) {

         StringBuffer bufferRes = null;

         try {

             TrustManager[] tm = { new MyX509TrustManager() };

             SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");

             sslContext.init(null, tm, new java.security.SecureRandom());

             // 從上述SSLContext對象中得到SSLSocketFactory對象  

             SSLSocketFactory ssf = sslContext.getSocketFactory();



             URL urlGet = new URL(url);

             HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();

             // 連接配接逾時

             http.setConnectTimeout(25000);

             // 讀取逾時 --伺服器響應比較慢,增大時間

             http.setReadTimeout(25000);

             http.setRequestMethod("POST");

             http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

             http.setSSLSocketFactory(ssf);

             http.setDoOutput(true);

             http.setDoInput(true);

             http.connect();



             OutputStream out = http.getOutputStream();

             out.write(params.getBytes("UTF-8"));

             out.flush();

             out.close();



             InputStream in = http.getInputStream();

             BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));

             String valueString = null;

             bufferRes = new StringBuffer();

             while ((valueString = read.readLine()) != null){

                 bufferRes.append(valueString);

             }

             in.close();

             if (http != null) {

                 // 關閉連接配接

                 http.disconnect();

             }

             return bufferRes.toString();

         } catch (Exception e) {

             e.printStackTrace();

             return null;

         }

     }



     /**

      * 構造請求參數

      * @param url

      * @param params

      * @return

      */

     public static String initParams(String url, Map<String, String> params){

         if (null == params || params.isEmpty()) {

             return url;

         }

         StringBuilder sb = new StringBuilder(url);

         if (url.indexOf("?") == -1) {

             sb.append("?");

         } else {

             sb.append("&");

         }

         boolean first = true;

         for (Entry<String, String> entry : params.entrySet()) {

             if (first) {

                 first = false;

             } else {

                 sb.append("&");

             }

             String key = entry.getKey();

             String value = entry.getValue();

             sb.append(key).append("=");

             if (StringUtils.isNotEmpty(value)) {

                 try {

                     sb.append(URLEncoder.encode(value, DEFAULT_CHARSET));

                 } catch (UnsupportedEncodingException e) {

                     e.printStackTrace();

                 }

             }

         }

         return sb.toString();

     }

 }



 // 證書管理

 class MyX509TrustManager implements X509TrustManager {



     public X509Certificate[] getAcceptedIssuers() {

         return null;  

     }



     @Override

     public void checkClientTrusted(X509Certificate[] chain, String authType)

             throws CertificateException {

     }



     @Override

     public void checkServerTrusted(X509Certificate[] chain, String authType)

             throws CertificateException {

     }

 }