Android-HttpsURLConnectionHelp工具类

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 params, String jsessionid){
        String responseContent = null;
        StringBuilder sb = new StringBuilder(urlStr);
        sb.append("?");
        for(Map.Entry 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;
    }
}

你可能感兴趣的:(Android)