httpclientutil

package com.qq.weixin.mp.aes;

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

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpUtils {

    public static String CONTENT_TYPE_FORM_URLENCODED = "application/x-www-form-urlencoded;charset=utf-8";

    public static String CONTENT_TYPE_FORM_DATA = "multipart/form-data;charset=utf-8";

    public static String CONTENT_TYPE_PLAIN = "text/plain;charset=utf-8";

    public static String CONTENT_TYPE_JSON = "application/json;charset=utf-8";

    protected final static Logger logger = LoggerFactory.getLogger(HttpUtils.class);

    /**
     * POST 請求 中間為url參數 後面為body裡面的參數,可以自己替換為json
     * 
     * @param url
     * @param params
     * @param sEncryptMsg
     * @return
     * @throws Exception
     * @throws IOException
     */
    public static String post(String url, Map<String, String> params, String sEncryptMsg) {
        StringBuilder parm = new StringBuilder();
        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                parm.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        /**
         * 拼接參數
         */
        url += parm;
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String respContent = null;
        StringEntity entity = new StringEntity(sEncryptMsg, "utf-8");// 解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpPost);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = httpResponse.getEntity();
                respContent = EntityUtils.toString(httpEntity, "UTF-8");
            }
        } catch (Exception e) {
        } finally {
            if (null != httpResponse) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return respContent;
    }

    
    /**
     * 发送get 请求  
     * @param url
     * @return
     */
    public static String doGet(String url) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
                    .setConnectionRequestTimeout(35000)// 请求超时时间
                    .setSocketTimeout(60000)// 数据读取超时时间
                    .build();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

相关jar包有 http-client 和 core 建议4.5 以上版本

posted @ 2019-01-21 17:02  cheakceep  阅读(165)  评论(0编辑  收藏  举报