http请求工具类HttpClientUtil(get使用body,post乱码问题解决)

最近很多发送http请求的需求存在,书写下util

1:配置需要的依赖

在pom.xml中配置http相关依赖

		
			org.apache.httpcomponents
			httpclient
			compile
		
		
			org.apache.httpcomponents
			httpcore
		

2:因部分特殊(蠢狗)对接方需要get请求使用body请求体,附上重写的类

/**
 * @author afresh
 * @description 封装httpget可以携带body参数
 * @since 2020/2/25 11:08 上午
 */
public class HttpGetWithBody extends HttpEntityEnclosingRequestBase {

    public static final String METHOD_NAME = "GET";

    @Override
    public String getMethod() { return METHOD_NAME; }

    public HttpGetWithBody(final String uri) {
        super();
        setURI(URI.create(uri));
    }
    public HttpGetWithBody(final URI uri) {
        super();
        setURI(uri);
    }
    public HttpGetWithBody() { super(); }
}

若其他请求例如delete也需要使用body请求体,则将METHD_NAME改为对应的值即可 例如:“DELETE”

3:完整util代码 可直接使用

注意一点:常见的post请求乱码问题需要使用 下述代码(只设置CONTENT_TYPE的编码格式不生效)

httpPost.setEntity(new StringEntity(param, ContentType.create("application/json", "utf-8")));
public class HttpCommonClientUtils {

    private static PoolingHttpClientConnectionManager pccm= null;

    private static final Logger logger = LoggerFactory.getLogger(HttpCommonClientUtils.class);

    private static final String CONTENT_TYPE = "application/json;charset=utf-8";

    static {
        try{
            //设置访问协议
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                //信任所有
                public boolean isTrusted(X509Certificate[] chain,
                                         String authType) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            Registry socketFactoryRegistry = RegistryBuilder.create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", sslsf)
                    .build();

            pccm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            pccm.setDefaultMaxPerRoute(30); //每个主机的最大并行链接数
            pccm.setMaxTotal(200);
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return
     */
    private static HttpClient getHttpClient() {

        //设置连接超时时间
        int REQUEST_TIMEOUT = 20*1000;  //设置请求超时20秒钟
        int SO_TIMEOUT = 20*1000;       //设置等待数据超时时间20秒钟
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setSocketTimeout(SO_TIMEOUT)
                .setConnectTimeout(REQUEST_TIMEOUT)
                .setConnectionRequestTimeout(REQUEST_TIMEOUT)
                .setStaleConnectionCheckEnabled(true)
                .build();

        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(pccm).setDefaultRequestConfig(defaultRequestConfig).build();
        return httpClient;
    }

    /**
     * 发起请求并返回结果POST
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String executePost(String url, Object params, String authorization)  throws  Exception {
        String result = null;
        String setUrl=url;
        String param= JSONObject.toJSONString(params);
        logger.info("请求url:"+url);
        logger.info("请求入参:"+param);
        HttpPost httpPost = new HttpPost(setUrl);
        httpPost.setEntity(new StringEntity(param, ContentType.create("application/json", "utf-8")));
        httpPost.setHeader("Content-Type","application/json");
        if(StringUtils.isNotBlank(authorization)) {
            httpPost.setHeader("Authorization",authorization);
        }
        try {
            HttpResponse response = getHttpClient().execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:"+statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_BAD_REQUEST) {
                result = getStreamAsString(response.getEntity().getContent(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(500, "网络繁忙, 请稍后再试");
        }
        return result;
    }


    /**
     * 发起请求并返回结果GET
     * @param url
     * @return
     * @throws Exception
     */
    public static String executeGet(String url, String authorization) throws BaseException {
        logger.info("请求url:"+url);
        HttpGet httpGet = new HttpGet(url);

        HttpResponse response = null;
        try {
            httpGet.setHeader("Content-Type",CONTENT_TYPE);
            if(StringUtils.isNotBlank(authorization)) {
                httpGet.setHeader("Authorization",authorization);
            }
            response = getHttpClient().execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:"+statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_BAD_REQUEST) {
                return getStreamAsString(response.getEntity().getContent(), "UTF-8");
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new BaseException(500, "网络繁忙, 请稍后再试");
        }
    }


    public static String executeGetWithParam(String url, Object param, String authorization){
        JSONObject params = (JSONObject) JSONObject.toJSON(param);
        logger.info("请求地址:{}, 请求参数:{}", url, params.toJSONString());

        StringBuffer paramsStr = new StringBuffer("?");
        String paramResult = null;
        if (!params.isEmpty()) {
            for (Map.Entry entry : params.entrySet()) {
                if (entry.getValue() != null){
                    paramsStr.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
            }
            paramResult = paramsStr.substring(0, paramsStr.length() - 1);
        }

        if (paramResult != null) {
            url = url + paramResult;
        }

        String result = null;
        try {
            result = HttpCommonClientUtils.executeGet(url,authorization);
        } catch (BaseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 发起请求并返回结果get(body请求体)
     * @param url
     * @return
     * @throws Exception
     */
    public static String executeGethWithBody(String url, Object params, String authorization)  throws  Exception {
        String result = null;
        HttpGetWithBody httpGet = new HttpGetWithBody(url);
        String param= JSONObject.toJSONString(params);
        logger.info("请求url:"+url);
        logger.info("请求入参:"+param);
        httpGet.setEntity(new StringEntity(param, ContentType.create("application/json", "utf-8")));
        httpGet.setHeader("Content-Type",CONTENT_TYPE);
        if(StringUtils.isNotBlank(authorization)) {
            httpGet.setHeader("Authorization",authorization);
        }
        try {
            HttpResponse response = getHttpClient().execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:"+statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
                result = getStreamAsString(response.getEntity().getContent(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(500, "网络繁忙, 请稍后再试");
        }
        return result;
    }

    /**
     * 发起请求并返回结果PUT
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String executePut(String url, Object params, String authorization)  throws  Exception {
        String result = null;
        String setUrl=url;
        String param= JSONObject.toJSONString(params);
        logger.info("请求url:"+url);
        logger.info("请求入参:"+param);
        HttpPut httpPut = new HttpPut(setUrl);
        httpPut.setEntity(new StringEntity(param,ContentType.create("application/json", "utf-8")));
        httpPut.setHeader("Content-Type",CONTENT_TYPE);
        if(StringUtils.isNotBlank(authorization)) {
            httpPut.setHeader("Authorization",authorization);
        }
        try {
            HttpResponse response = getHttpClient().execute(httpPut);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:"+statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
                result = getStreamAsString(response.getEntity().getContent(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(500, "网络繁忙, 请稍后再试");
        }
        return result;
    }


    /**
     * 发起请求并返回结果DELETE
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String executeDel(String url, Object params, String authorization)  throws  Exception {
        String result = null;
        String setUrl=url;
        String param= JSONObject.toJSONString(params);
        HttpDelete httpdelete = new HttpDelete(setUrl);
        httpdelete.setHeader("Content-Type",CONTENT_TYPE);
        if(StringUtils.isNotBlank(authorization)) {
            httpdelete.setHeader("Authorization",authorization);
        }
        try {
            HttpResponse response = getHttpClient().execute(httpdelete);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:"+statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
                result = getStreamAsString(response.getEntity().getContent(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(500, "网络繁忙, 请稍后再试");
        }
        return result;
    }

    /**
     * 发起请求并返回结果DELETE(body请求体)
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String executeDelWithBody(String url, Object params, String authorization)  throws  Exception {
        String result = null;
        String setUrl=url;
        String param= JSONObject.toJSONString(params);
        HttpDeleteWithBody httpdelete = new HttpDeleteWithBody(setUrl);
        httpdelete.setEntity(new StringEntity(param,ContentType.create("application/json", "utf-8")));
        httpdelete.setHeader("Content-Type",CONTENT_TYPE);
        if(StringUtils.isNotBlank(authorization)) {
            httpdelete.setHeader("Authorization",authorization);
        }
        try {
            HttpResponse response = getHttpClient().execute(httpdelete);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:"+statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
                result = getStreamAsString(response.getEntity().getContent(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(500, "网络繁忙, 请稍后再试");
        }
        return result;
    }

    /**
     * 发起请求并返回结果PATCH
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String executePatch(String url, Object params, String authorization)  throws  Exception {
        String result = null;
        String setUrl=url;
        String param= JSONObject.toJSONString(params);
        HttpPatch httpPatch = new HttpPatch(setUrl);
        httpPatch.setEntity(new StringEntity(param,ContentType.create("application/json", "utf-8")));
        httpPatch.setHeader("Content-Type",CONTENT_TYPE);
        if(StringUtils.isNotBlank(authorization)) {
            httpPatch.setHeader("Authorization",authorization);
        }
        try {
            HttpResponse response = getHttpClient().execute(httpPatch);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:"+statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
                result = getStreamAsString(response.getEntity().getContent(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(500, "网络繁忙, 请稍后再试");
        }
        return result;
    }

    /**
     * 将流转换为字符串
     * @param stream
     * @param charset
     * @return
     * @throws IOException
     */
    private static String getStreamAsString(InputStream stream, String charset) throws IOException {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset), 8192);
            StringWriter writer = new StringWriter();

            char[] chars = new char[8192];
            int count = 0;
            while ((count = reader.read(chars)) > 0) {
                writer.write(chars, 0, count);
            }

            return writer.toString();
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }

    /**
     *
     * @param requestParam
     * @param coder
     * @return
     */
    private static String getRequestParamString(Map requestParam, String coder) {
        if (null == coder || "".equals(coder)) {
            coder = "UTF-8";
        }
        StringBuffer sf = new StringBuffer("");
        String reqstr = "";
        if (null != requestParam && 0 != requestParam.size()) {
            for (Map.Entry en : requestParam.entrySet()) {
                try {
                    sf.append(en.getKey()
                            + "="
                            + (null == en.getValue() || "".equals(en.getValue()) ? "" : URLEncoder
                            .encode(en.getValue(), coder)) + "&");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    return "";
                }
            }
            reqstr = sf.substring(0, sf.length() - 1);
        }
        return reqstr;
    }
}

 

你可能感兴趣的:(后端)