com.mingsoft.weixin.http.HttpClientConnectionManager.java Source code

Java tutorial

Introduction

Here is the source code for com.mingsoft.weixin.http.HttpClientConnectionManager.java

Source

/**
The MIT License (MIT) * Copyright (c) 2016 (mingsoft.net)
    
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
    
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
    
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.mingsoft.weixin.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * Copyright: Copyright (c) 2014 - 2015
 * Company:??
 * @author wangtp
 * @version 300-001-001
 * ? 
 * Comments:
 * ,com.mingsoft.weixin???httpclient
 * ???utils
 * Create Date:2013-12-23
 * Modification history:
 */
public class HttpClientConnectionManager {

    /**
     * ?UTF-8
     */
    public final static String UTF8 = "UTF-8";
    /**
     * ?GB2312
     */
    public final static String GB2312 = "GB2312";

    public final static String POST = "POST";

    public final static String GET = "GET";

    /**
     * ?SSL?HttpClient
     * 
     * @param httpClient
     * @return
     */
    public static HttpClient getSSLInstance(HttpClient httpClient) {
        ClientConnectionManager ccm = httpClient.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, WeixinSSLSocketFactory.getInstance()));
        httpClient = new DefaultHttpClient(ccm, httpClient.getParams());
        return httpClient;
    }

    /**
     * ?
     * @param postUrl ?
     * @param param ?
     * @param method 
     * @return null
     */
    public static String request(String postUrl, String param, String method) {
        URL url;
        try {
            url = new URL(postUrl);

            HttpURLConnection conn;
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(30000); // ??)
            conn.setReadTimeout(30000); // ????)
            conn.setDoOutput(true); // post??http?truefalse
            conn.setDoInput(true); // ?httpUrlConnectiontrue
            conn.setUseCaches(false); // Post ?
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestMethod(method);// "POST"GET
            conn.setRequestProperty("Content-Length", param.length() + "");
            String encode = "utf-8";
            OutputStreamWriter out = null;
            out = new OutputStreamWriter(conn.getOutputStream(), encode);
            out.write(param);
            out.flush();

            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return null;
            }
            // ??
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line = "";
            StringBuffer strBuf = new StringBuffer();
            while ((line = in.readLine()) != null) {
                strBuf.append(line).append("\n");
            }
            in.close();
            out.close();
            return strBuf.toString();
        } catch (MalformedURLException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * ?post??
     * 
     * @param url
     * @return
     */
    public static HttpPost getPostMethod(String url) {

        URI uri = null;
        try {
            URL _url = new URL(url);
            uri = new URI(_url.getProtocol(), _url.getHost(), _url.getPath(), _url.getQuery(), null);

        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        HttpPost pmethod = new HttpPost(uri); // ??
        pmethod.addHeader("Connection", "keep-alive");
        pmethod.addHeader("Accept", "*/*");
        pmethod.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        pmethod.addHeader("Host", "mp.weixin.qq.com");
        pmethod.addHeader("X-Requested-With", "XMLHttpRequest");
        pmethod.addHeader("Cache-Control", "max-age=0");
        pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
        return pmethod;
    }

    /**
     * ?GET??
     * 
     * @param url
     * @return
     */
    public static HttpGet getGetMethod(String url) {
        URI uri = null;
        try {
            URL _url = new URL(url);
            uri = new URI(_url.getProtocol(), _url.getHost(), _url.getPath(), _url.getQuery(), null);

        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        HttpGet pmethod = new HttpGet(uri);
        // ??
        pmethod.addHeader("Connection", "keep-alive");
        pmethod.addHeader("Cache-Control", "max-age=0");
        pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
        pmethod.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8");
        return pmethod;
    }
}