1. 程式人生 > >詳解http與https的區別

詳解http與https的區別

複製程式碼
public class HttpsUtil {
    static TrustManager[] xtmArray = new MytmArray[] { new MytmArray() };// 建立信任規則列表
    private final static int CONNENT_TIMEOUT = 15000;
    private final static int READ_TIMEOUT = 15000;
    static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            
return true; } }; /** * 信任所有主機-對於任何證書都不做檢查 Create a trust manager that does not validate * certificate chains, Android 採用X509的證書資訊機制,Install the all-trusting trust * manager */ private static void trustAllHosts() { try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(
null, xtmArray, new java.security.SecureRandom()); HttpsURLConnection .setDefaultSSLSocketFactory(sc.getSocketFactory()); // 不進行主機名確認,對所有主機 HttpsURLConnection.setDefaultHostnameVerifier(DO_NOT_VERIFY); } catch (Exception e) { e.printStackTrace(); } }
// /** // * https get方法,返回值是https請求,服務端返回的資料string型別,資料進行xml解析 // * */ // public static String HttpsGet(String httpsurl) { // return HttpsPost(httpsurl, null); // // } /** * https post方法,返回值是https請求,服務端返回的資料string型別,資料進行xml解析 * */ public static String HttpsPost(String httpsurl, String data) { String result = null; HttpURLConnection http = null; URL url; try { url = new URL(httpsurl); // 判斷是http請求還是https請求 if (url.getProtocol().toLowerCase().equals("https")) { trustAllHosts(); http = (HttpsURLConnection) url.openConnection(); ((HttpsURLConnection) http).setHostnameVerifier(DO_NOT_VERIFY);// 不進行主機名確認 } else { http = (HttpURLConnection) url.openConnection(); } http.setConnectTimeout(CONNENT_TIMEOUT);// 設定超時時間 http.setReadTimeout(READ_TIMEOUT); if (data == null) { http.setRequestMethod("GET");// 設定請求型別 http.setDoInput(true); // http.setRequestProperty("Content-Type", "text/xml"); if (AppSession.mCookieStore != null) http.setRequestProperty("Cookie", AppSession.mCookieStore); } else { http.setRequestMethod("POST");// 設定請求型別為post http.setDoInput(true); http.setDoOutput(true); // http.setRequestProperty("Content-Type", "text/xml"); if (AppSession.mCookieStore != null && AppSession.mCookieStore.trim().length() > 0) http.setRequestProperty("Cookie", AppSession.mCookieStore); DataOutputStream out = new DataOutputStream( http.getOutputStream()); out.writeBytes(data); out.flush(); out.close(); } // 設定http返回狀態200(ok)還是403 AppSession.httpsResponseCode = http.getResponseCode(); BufferedReader in = null; if (AppSession.httpsResponseCode == 200) { getCookie(http); in = new BufferedReader(new InputStreamReader( http.getInputStream())); } else in = new BufferedReader(new InputStreamReader( http.getErrorStream())); String temp = in.readLine(); while (temp != null) { if (result != null) result += temp; else result = temp; temp = in.readLine(); } in.close(); http.disconnect(); } catch (Exception e) { e.printStackTrace(); } return result; } /** * 得到cookie * */ private static void getCookie(HttpURLConnection http) { String cookieVal = null; String key = null; AppSession.mCookieStore = ""; for (int i = 1; (key = http.getHeaderFieldKey(i)) != null; i++) { if (key.equalsIgnoreCase("set-cookie")) { cookieVal = http.getHeaderField(i); cookieVal = cookieVal.substring(0, cookieVal.indexOf(";")); AppSession.mCookieStore = AppSession.mCookieStore + cookieVal + ";"; } } } }
複製程式碼