Android-HttpsURLConnectionHelp-工具類
阿新 • • 發佈:2018-12-24
HttpsURLConnectionHelp-工具類 是專門把javax.net.ssl.HttpsURLConnection類的使用,進行了封裝,提供了常用的公共方法;
package common.library.utils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; importjava.net.URL; import java.security.cert.CertificateException; import java.util.Map; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager; /** * @Author Liudeli * @Describe:Java版(HttpURLConnection)Https請求工具類 */ public class HttpsURLConnectionHelp { /** * 加密協議 */ public static TrustManager truseAllManager = new X509TrustManager() { public void checkClientTrusted( java.security.cert.X509Certificate[] arg0, String arg1)throws CertificateException { } public void checkServerTrusted( java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException { } public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } }; public static String requesByGetToString(String url, String jsessionid) { String message = null; try { HttpsURLConnection conn = getHttpsConnection(url); conn.setRequestProperty("cookie", jsessionid); // 請求成功(相應碼 == 200) if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { // 取得該連線的輸入流,以讀取響應內容 InputStreamReader insr = new InputStreamReader( conn.getInputStream()); StringBuffer strb = new StringBuffer(); int s; while ((s = insr.read()) != -1) { strb.append((char) s); } message = strb.toString(); ELog.i("", message); } } catch (Exception e) { e.printStackTrace(); } return message; } public static InputStream requesByGetToStream(String url) { InputStream inputStream = null; try { HttpsURLConnection conn = getHttpsConnection(url); //自動重定向新地址 conn.setInstanceFollowRedirects(true); // 請求成功(相應碼 == 200) if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { // 取得該連線的輸入流,以讀取響應內容 inputStream = conn.getInputStream(); } } catch (Exception e) { e.printStackTrace(); } return inputStream; } public static Object[] requesByGetToBitmap(String url) { Object[] objects = null; try { HttpsURLConnection conn = getHttpsConnection(url); //自動重定向新地址 conn.setInstanceFollowRedirects(true); // 請求成功(相應碼 == 200) if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { objects = new Object[2]; String cookieval = conn.getHeaderField("set-cookie"); String jsessionid = cookieval.substring(0, cookieval.indexOf(";")); InputStream is = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); objects[0] = jsessionid; objects[1] = bitmap; } } catch (Exception e) { e.printStackTrace(); } return objects; } public String requesByGetToParams(String urlStr, Map<String,String> params, String jsessionid){ String responseContent = null; StringBuilder sb = new StringBuilder(urlStr); sb.append("?"); for(Map.Entry<String, String> entry:params.entrySet()){ sb.append(entry.getKey()); sb.append("="); //防止中文亂碼 sb.append(entry.getValue()); sb.append("&"); } sb.deleteCharAt(sb.length() -1); try { HttpsURLConnection conn = getHttpsConnection(urlStr); conn.setRequestProperty("cookie", jsessionid); //請求成功(相應碼 == 200) if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){ InputStream is = conn.getInputStream(); int s; StringBuffer strb = new StringBuffer(); while((s = is.read()) != -1){ strb.append((char)s); } responseContent = strb.toString(); } } catch (Exception e) { e.printStackTrace(); } return responseContent; } public static HttpsURLConnection getHttpsConnection(String urlStr) { // 從上述SSLContext物件中得到SSLSocketFactory物件 HttpsURLConnection conn = null; try { // 建立SSLContext物件,並使用我們指定的信任管理器初始化 SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[] { truseAllManager }, null); SSLSocketFactory ssf = sslContext.getSocketFactory(); URL myURL = new URL(urlStr); conn = (HttpsURLConnection) myURL.openConnection(); //設定加密協議 conn.setSSLSocketFactory(ssf); //設定請求方式 conn.setRequestMethod("GET"); //設定連線超時時長 conn.setConnectTimeout(15000); conn.setReadTimeout(15000); conn.setHostnameVerifier(new HostnameVerifier(){ public boolean verify(String hostname, SSLSession session) { return true; }}); } catch (Exception e) { e.printStackTrace(); } return conn; } }