java HttpClient请求接口

版权声明:转载博主原创文章,请注明文章来源地址! https://blog.csdn.net/qq_38428623/article/details/82983991

public class HttpUtil {

    /**
     * 获取请求的客户端
     *
     * @return
     */
    private static HttpClient getHttpClient() {
        HttpClientBuilder builder = HttpClientBuilder.create();
        HttpClient httpClient = builder.build();
        return httpClient;
    }

    /**
     * 获取配置信息
     *
     * @return
     */
    private static RequestConfig getRequestConfig() {
        return RequestConfig.custom()
                .setConnectTimeout(3000).setConnectionRequestTimeout(3000)
                .setSocketTimeout(3000).build();
    }

    /**
     * httpGet
     *
     * @param url       url
     * @param headerMap headerMap
     * @return
     */
    public static String httpGet(String url, Map headerMap) {
        try {
            HttpClient httpClient = getHttpClient();
            HttpGet httpGet = new HttpGet(url);
            httpGet.setConfig(getRequestConfig());
            for (Object key : headerMap.keySet()) {
                httpGet.addHeader(key.toString(), headerMap.get(key).toString());
            }
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity entity = httpResponse.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException("调用接口异常:".concat(e.getMessage()));
        }
    }


    /**
     * httpPost
     *
     * @param url       url
     * @param headerMap headerMap
     * @param params    params
     * @return
     */
    public static String httpPost(String url, Map headerMap, List params) {
        try {
            HttpClient httpClient = getHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(getRequestConfig());
            httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            for (Object key : headerMap.keySet()) {
                httpPost.addHeader(key.toString(), headerMap.get(key).toString());
            }
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException("调用接口异常:".concat(e.getMessage()));
        }
    }

    /**
     * httpPost
     *
     * @param url       url
     * @param headerMap headerMap
     * @return
     */
    public static String httpDelete(String url, Map headerMap) {
        try {
            HttpClient httpClient = getHttpClient();
            HttpDelete httpDelete = new HttpDelete(url);
            httpDelete.setConfig(getRequestConfig());
            for (Object key : headerMap.keySet()) {
                httpDelete.addHeader(key.toString(), headerMap.get(key).toString());
            }
            HttpResponse httpResponse = httpClient.execute(httpDelete);
            HttpEntity entity = httpResponse.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException("调用接口异常:".concat(e.getMessage()));
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38428623/article/details/82983991