httpClient通用工具類

工具類:

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URISyntaxException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.Charsets;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientUtils {
    private static PoolingHttpClientConnectionManager cm;
    private static String UTF_8 = "UTF-8";

    private static CloseableHttpClient httpClient;

    static{
        init();
    }


    private static void init() {
        if (cm == null) {
            cm = new PoolingHttpClientConnectionManager();
            cm.setMaxTotal(50);// 整個連接池最大連接數(shù)
            cm.setDefaultMaxPerRoute(5);// 每路由最大連接數(shù),默認值是2
        }
        httpClient=HttpClients.custom().setConnectionManager(cm).build();
    }
    /**
     * 通過連接池獲取HttpClient
     *
     * @return
     */
    private static CloseableHttpClient getHttpClient() {
//        RequestConfig config = RequestConfig.custom().setRedirectsEnabled(true).build();//允許重定向
//        return HttpClients.custom().setDefaultRequestConfig(config).setConnectionManager(cm).build();
//        return HttpClients.custom().setConnectionManager(cm).build();
        return httpClient;
    }

    /**
     *
     * @param url
     * @return
     */
    public static String get(String url) {
        HttpGet httpGet = new HttpGet(url);
        return getResult(httpGet);
    }

    /**
     *
     * @param url
     * @return
     */
    public static byte[] getBinary(String url) {
        HttpGet httpGet = new HttpGet(url);
        return getBinaryResult(httpGet);
    }

    public static String get(String url, Map<String, Object> params) throws URISyntaxException {
        URIBuilder ub = new URIBuilder();
        ub.setPath(url);

        ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
        ub.setParameters(pairs);

        HttpGet httpGet = new HttpGet(ub.build());
        return getResult(httpGet);
    }

    public static String get(String url, Map<String, Object> headers, Map<String, Object> params)
            throws URISyntaxException {
        URIBuilder ub = new URIBuilder();
        ub.setPath(url);

        if (params!=null) {
            ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
            ub.setParameters(pairs);
        }
        HttpGet httpGet = new HttpGet(ub.build());
        for (Map.Entry<String, Object> param : headers.entrySet()) {
            httpGet.addHeader(param.getKey(), String.valueOf(param.getValue()));
        }
        return getResult(httpGet);
    }

    public static String post(String url) {
        HttpPost httpPost = new HttpPost(url);
        return getResult(httpPost);
    }

    public static String post(String url, Map<String, Object> params) throws UnsupportedEncodingException {
        HttpPost httpPost = new HttpPost(url);
        ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
        httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8));
        return getResult(httpPost);
    }

    public static String post(String url, Map<String, Object> headers, Map<String, Object> params)
            throws UnsupportedEncodingException {
        HttpPost httpPost = new HttpPost(url);

        for (Map.Entry<String, Object> param : headers.entrySet()) {
            httpPost.addHeader(param.getKey(), String.valueOf(param.getValue()));
        }

        ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
        httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8));

        return getResult(httpPost);
    }

    private static ArrayList<NameValuePair> covertParams2NVPS(Map<String, Object> params) {
        ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
        for (Map.Entry<String, Object> param : params.entrySet()) {
            pairs.add(new BasicNameValuePair(param.getKey(), String.valueOf(param.getValue())));
        }
        return pairs;
    }

    /**
     * 處理Http請求
     *
     * @param request
     * @return
     */
    private static String getResult(HttpRequestBase request) {
        // CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpClient httpClient = getHttpClient();
        try {

            CloseableHttpResponse response = httpClient.execute(request);
            // response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();

            //多次重定向處理
            if ( response.getStatusLine().getStatusCode() == 302) {
                Header[] header = response.getHeaders("location");
                return get(header[0].getValue());
            }

            if (entity != null) {
                // long len = entity.getContentLength();// -1 表示長度未知
                String result = EntityUtils.toString(entity,"UTF-8");
                response.close();
                // httpClient.close();
                return result;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return "";
    }

    /**
     * 處理Http請求 ,返回二進制數(shù)據(jù)
     *
     * @param request
     * @return
     */
    private static byte[] getBinaryResult(HttpRequestBase request) {
        // CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpClient httpClient = getHttpClient();
        try {

            CloseableHttpResponse response = httpClient.execute(request);
            // response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();

            //多次重定向處理
            if ( response.getStatusLine().getStatusCode() == 302) {
                Header[] header = response.getHeaders("location");
                return getBinary(header[0].getValue());
            }

            if (entity != null) {
                // long len = entity.getContentLength();// -1 表示長度未知
                byte[] result = EntityUtils.toByteArray(entity);
                response.close();
                // httpClient.close();
                return result;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return null;
    }


    /**
     * post 請求
     * @param url
     * @param params
     * @param headers
     * @return
     * @throws Exception
     */
    public static String httpPostRequest(String url, String params, Map<String, Object> headers) throws Exception {
        HttpPost httpPost = new HttpPost(url);
        if(null != headers){
            for (Map.Entry<String, Object> param : headers.entrySet()) {
                httpPost.setHeader(param.getKey(), String.valueOf(param.getValue()));
            }
        }
        StringEntity se = new StringEntity(params, "UTF-8");
        //se.setContentType("application/json");
        httpPost.setEntity(se);
        return getResult(httpPost);
    }
    public static String sendHttpPost(String url, String body) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setEntity(new StringEntity(body, Charsets.UTF_8));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        System.out.println(response.getStatusLine().getStatusCode() + "\n");
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
        response.close();
        httpClient.close();
        return responseContent;
    }
}

使用示例:
post請求:

Map<String,Object> map = new HashMap<>();
 map.put("a",a);
        map.put("b",b);
HttpClientUtils.sendHttpPost(URL,JSONObject.toJSONString(map));

使用如上請求方式,接收方可以用以下接收參數(shù),只舉例一種,辦法很多,隨心而欲。

@RequestMapping(value = "jiekou",method= RequestMethod.POST ,produces = "application/json;charset=utf-8")
    public void heartCallBack(HttpServletRequest request){
  request.setCharacterEncoding("UTF-8");
 String param=null;
        param = getBodyData(request);
}
============獲取參數(shù)方法=============
//獲取請求體中的字符串(POST)
    private static String getBodyData(HttpServletRequest request) {
        StringBuffer data = new StringBuffer();
        String line = null;
        BufferedReader reader = null;
        try {
            reader = request.getReader();
            while (null != (line = reader.readLine()))
                data.append(line);
        } catch (IOException e) {
        } finally {
        }
        return data.toString();
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容