post 封裝Map 傳送請求

weixin_33894992發表於2016-12-28
package com.j1.weixin.util;


import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;


public class HttpUtils {
    /**
     * 傳送HTTP請求
     *
     * @param url
     * @param propsMap 傳送的引數
     */
    public static HttpResponse httpPost(String url, Map<String, Object> propsMap) {
        HttpResponse response = new HttpResponse();
        String responseMsg = "";

        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);// POST請求
        if (propsMap != null) {
            // 引數設定
            Set<String> keySet = propsMap.keySet();
            NameValuePair[] postData = new NameValuePair[keySet.size()];
            int index = 0;
            for (String key : keySet) {
                postData[index++] = new NameValuePair(key, propsMap.get(key)
                        .toString());
            }
            postMethod.addParameters(postData);
        }
        postMethod.getParams().setParameter(
                HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        try {
            int statusCode = httpClient.executeMethod(postMethod);// 傳送請求
            response.setStatusCode(statusCode);
            if (statusCode == HttpStatus.SC_OK) {
                // 讀取內容
                byte[] responseBody = postMethod.getResponseBody();
                // 處理返回的內容
                responseMsg = new String(responseBody, "utf-8");
            }
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            postMethod.releaseConnection();// 關閉連線
        }
        response.setContent(responseMsg);
        return response;
    }

    /**
     * 傳送HTTP請求
     *
     * @param url
     */
    public static HttpResponse httpGet(String url) {
        HttpResponse response = new HttpResponse();
        String responseMsg = "";

        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(url);

        try {
            int statusCode = httpClient.executeMethod(getMethod);// 傳送請求
            response.setStatusCode(statusCode);
            if (statusCode == HttpStatus.SC_OK) {
                // 讀取內容
                byte[] responseBody = getMethod.getResponseBody();
                // 處理返回的內容
                responseMsg = new String(responseBody, "utf-8");
            }
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            getMethod.releaseConnection();// 關閉連線
        }
        response.setContent(responseMsg);
        return response;
    }
    
    /**
     * 傳送HTTP--GET請求
     * 
     * @param url
     * @param propsMap
     *            傳送的引數
     */
    public static String httpGetSend(String url) {
        String responseMsg = "";
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(url);// GET請求
        try {
            // http超時5秒
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            // 設定 get 請求超時為 5 秒
            getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
            getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            httpClient.executeMethod(getMethod);// 傳送請求
            // 讀取內容
            byte[] responseBody = getMethod.getResponseBody();
            // 處理返回的內容
            responseMsg = new String(responseBody, "utf-8");
        } catch (Exception e) {
            Logger.getLogger(HttpUtils.class).error(e.getMessage());
            e.printStackTrace();
        } finally {
            getMethod.releaseConnection();// 關閉連線
        }
        return responseMsg;
    }
    
    
    /**
     * 傳送HTTP請求
     * 
     * @param url
     * @param propsMap
     *            傳送的引數
     */
    public static String httpSend(String url, Map<String, Object> propsMap) {
        String responseMsg = "";

        HttpClient httpClient = new HttpClient();
        
        PostMethod postMethod = new PostMethod(url);// POST請求
        
//        postMethod.
        // 引數設定
        Set<String> keySet = propsMap.keySet();
        NameValuePair[] postData = new NameValuePair[keySet.size()];
        int index = 0;
        for (String key : keySet) {
            postData[index++] = new NameValuePair(key, propsMap.get(key)
                    .toString());
        }
        postMethod.addParameters(postData);
        try {
            httpClient.executeMethod(postMethod);// 傳送請求
//            Log.info(postMethod.getStatusCode());
            // 讀取內容
            byte[] responseBody = postMethod.getResponseBody();
            // 處理返回的內容
            responseMsg = new String(responseBody);

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            postMethod.releaseConnection();// 關閉連線
        }
        return responseMsg;
    }
    
    
    /**
     * 傳送Post HTTP請求
     * 
     * @param url
     * @param propsMap
     *            傳送的引數
     * @throws IOException 
     * @throws HttpException 
     */
    public static PostMethod httpSendPost(String url, Map<String, Object> propsMap,String authrition) throws Exception {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);// POST請求
        postMethod.addRequestHeader("Authorization",authrition);
        postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");  
        // 引數設定
        Set<String> keySet = propsMap.keySet();
        NameValuePair[] postData = new NameValuePair[keySet.size()];
        int index = 0;
        for (String key : keySet) {
            postData[index++] = new NameValuePair(key, propsMap.get(key)
                    .toString());
        }
        postMethod.addParameters(postData);
        
        httpClient.executeMethod(postMethod);// 傳送請求
        return postMethod;
    }
    
    /**
     * 傳送Post HTTP請求
     * 
     * @param url
     * @param propsMap
     *            傳送的引數
     * @throws IOException 
     * @throws HttpException 
     */
    public static PostMethod httpSendPost(String url, Map<String, Object> propsMap) throws Exception {
        String responseMsg = "";
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);// POST請求
        postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");  
        // 引數設定
        Set<String> keySet = propsMap.keySet();
        NameValuePair[] postData = new NameValuePair[keySet.size()];
        int index = 0;
        for (String key : keySet) {
            postData[index++] = new NameValuePair(key, propsMap.get(key)
                    .toString());
        }
        postMethod.addParameters(postData);
        
        httpClient.executeMethod(postMethod);// 傳送請求
        return postMethod;
    }
    
    
    /**
     * 傳送Get HTTP請求
     * 
     * @param url
     * @param propsMap
     *            傳送的引數
     * @throws IOException 
     * @throws HttpException 
     */
    public static GetMethod httpSendGet(String url, Map<String, Object> propsMap) throws Exception {
        String responseMsg = "";
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(url);// GET請求
        getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");  
        // 引數設定
        Set<String> keySet = propsMap.keySet();
        NameValuePair[] postData = new NameValuePair[keySet.size()];
        int index = 0;
        for (String key : keySet) {
            getMethod.getParams().setParameter(key, propsMap.get(key)
                    .toString());
        }
        
        httpClient.executeMethod(getMethod);// 傳送請求
        return getMethod;
    }
    
}

 

相關文章