1. 程式人生 > >java傳送https的請求

java傳送https的請求

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.cert.CertificateException;
 
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;
import java.security.cert.X509Certificate;
public class HttpsGetData {
private static class TrustAnyTrustManager implements X509TrustManager {
         
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }
  
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }
  
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }
    }
  
    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }
    public void HttpsGet(){
    try {
	SSLContext sc = SSLContext.getInstance("SSL");
	sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
				new java.security.SecureRandom());
	URL url = new URL(
					"https://www.xxxx.cn/api/check/"
							+ URLEncoder.encode(realName, "utf-8") + "/"
							+ cardNo);//此處中文亂碼
	HttpsURLConnection connection = (HttpsURLConnection) url
					.openConnection();
			//設定https相關屬性
			connection.setSSLSocketFactory(sc.getSocketFactory());
			connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
			connection.setDoOutput(true);
			//connection.setDoInput(true);

			// 設定通用的請求屬性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("Accept-Language", "zh-CN");
			connection.setRequestProperty("Accept-Charset", "UTF-8");
			connection.setRequestProperty("contentType", "UTF-8");
			connection
					.setRequestProperty("user-agent",
							"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			connection.setRequestMethod("GET");
			connection.connect();
			// 定義 BufferedReader輸入流來讀取URL的響應
			BufferedReader in = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), "UTF-8"));
			String line;
			String result = "";
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
                System.out.println("傳送GET請求出現異常!" + e);
                e.printStackTrace();
            }
            // 使用finally塊來關閉輸入流
            finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
    }
}

參考:

http://www.open-open.com/code/view/1432630203379

http://evan0625.iteye.com/blog/2064992