All Downloads are FREE. Search and download functionalities are using the official Maven repository.

in.hocg.boot.cps.autoconfiguration.impl.dataoke.lib.HttpUtil Maven / Gradle / Ivy

package in.hocg.boot.cps.autoconfiguration.impl.dataoke.lib;

import cn.hutool.core.util.StrUtil;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
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;
import org.apache.logging.log4j.util.Strings;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


public class HttpUtil {
    private static PoolingHttpClientConnectionManager cm;
    private static String EMPTY_STR = "";
    private static String UTF_8 = "UTF-8";

    private static void init() {
        if (cm == null) {
            cm = new PoolingHttpClientConnectionManager();
            // 整个连接池最大连接数
            cm.setMaxTotal(1000);
            // 每路由最大连接数,默认值是2
            cm.setDefaultMaxPerRoute(cm.getMaxTotal());
        }
    }

    /**
     * 通过连接池获取HttpClient
     *
     * @return
     */
    private static CloseableHttpClient getHttpClient() {
        init();
        return HttpClients.custom().setRetryHandler((exception, executionCount, context) -> {
            if (executionCount >= 1) {
                // Do not retry if over max retry count
                return false;
            }
            return true;
        }).setConnectionManager(cm).setConnectionManagerShared(true).build();
    }

    /**
     * @param url
     * @return
     */
    public static String httpGetRequest(String url) {
        HttpGet httpGet = new HttpGet(url);
        RequestConfig config = RequestConfig.custom()
            .setConnectTimeout(5000)   //设置连接超时时间
            .setConnectionRequestTimeout(5000) // 设置请求超时时间
            .setSocketTimeout(5000)
            .setRedirectsEnabled(false)//默认允许自动重定向
            .build();
        httpGet.setConfig(config);
        httpGet.setHeader("Accept", "application/json");
        httpGet.addHeader("Accept-Encoding", "gzip"); //请求使用数据压缩
        return getResult(httpGet);
    }

    public static String httpGetRequest(String url, Map params) throws URISyntaxException {
        String paramsStr = covertParamsForStr(params);
        url = url.contains("?") ? url : url + "?";
        HttpGet httpGet = new HttpGet(url + paramsStr);
        RequestConfig config = RequestConfig.custom()
            .setConnectTimeout(5000)   //设置连接超时时间
            .setConnectionRequestTimeout(5000) // 设置请求超时时间
            .setSocketTimeout(5000)
            .setRedirectsEnabled(false)//默认允许自动重定向
            .build();
        httpGet.setConfig(config);
        httpGet.setHeader("Accept", "application/json");
        httpGet.addHeader("Accept-Encoding", "gzip"); //请求使用数据压缩
        return getResult(httpGet);
    }

    public static String httpGetRequest(String url, Map headers, Map params)
        throws URISyntaxException {
        URIBuilder ub = new URIBuilder();
        ub.setPath(url);
        ArrayList pairs = covertParams2NVPS(params);
        ub.setParameters(pairs);
        HttpGet httpGet = new HttpGet(ub.build());
        for (Map.Entry param : headers.entrySet()) {
            httpGet.addHeader(param.getKey(), String.valueOf(param.getValue()));
        }
        return getResult(httpGet);
    }

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

    public static String httpPostRequest(String url, String json) {
        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(json, "utf-8");//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        return getResult(httpPost);
    }

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

    public static String httpPostRequest(String url, Map headers, Map params)
        throws UnsupportedEncodingException {
        HttpPost httpPost = new HttpPost(url);
        for (Map.Entry param : headers.entrySet()) {
            httpPost.addHeader(param.getKey(), String.valueOf(param.getValue()));
        }
        ArrayList pairs = covertParams2NVPS(params);
        httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8));

        return getResult(httpPost);
    }

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

    private static ArrayList covertParams2NVPSForStr(Map params) {
        ArrayList pairs = new ArrayList<>();
        for (Map.Entry param : params.entrySet()) {
            pairs.add(new BasicNameValuePair(param.getKey(), HttpUtil.encodeStr(param.getValue())));
        }
        return pairs;
    }

    private static String covertParamsForStr(Map paraMap) {
        if (paraMap == null) {
            paraMap = new HashMap<>();
        }
        paraMap = new TreeMap<>(paraMap);
        StringBuilder sb = new StringBuilder();
        paraMap.forEach((key, value) -> {
            sb.append(key);
            sb.append("=");
            sb.append(HttpUtil.encodeStr(value));
            sb.append("&");
        });
        return sb.toString();
    }

    /**
     * 处理Http请求
     */
    private static String getResult(HttpRequestBase request) {
        CloseableHttpClient httpClient = getHttpClient();
        String result = "";
        try {
            CloseableHttpResponse response = httpClient.execute(request);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
            response.close();
        } catch (IOException e) {
            //e.printStackTrace();
        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close(); //释放资源
                }
            } catch (Exception ignored) {

            }
        }
        return result;
    }


    public static String encodeStr(String value) {
        if (StrUtil.isBlank(value)) {
            return Strings.EMPTY;
        }
        try {
            return URLEncoder.encode(value, StandardCharsets.UTF_8);
        } catch (Exception e) {
            return Strings.EMPTY;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy