Java实现HttpClientUtil工具类

文章目录


代码

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

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

@Slf4j
public class HttpClientUtil {
    private static final RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(60000) // 60s
            .setConnectTimeout(10000) // 10s
            .setConnectionRequestTimeout(10000) // 10s
            .build();


    public static String sendHttpPost(String httpUrl, String body, Map<String, String> header) {
        HttpPost httpPost = new HttpPost(httpUrl);
        try {
            if (StringUtils.isNotEmpty(body)) {
                StringEntity stringEntity = new StringEntity(body, "UTF-8");
                stringEntity.setContentType("application/json");
                httpPost.setEntity(stringEntity);
            }
            if (CollectionUtil.isNotEmpty(header)) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpPost.addHeader(entry.getKey(), entry.getValue());
                }
            }
        } catch (Exception e) {
            log.error("httpUrl is {}, body is {}, header is {}, error: ", httpUrl, body, header.toString(), e);
        }
        return sendHttpPost(httpPost);
    }

    private static String sendHttpPost(HttpPost httpPost) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String responseContent = "";
        try {
            httpClient = HttpClients.createDefault();
            httpPost.setConfig(requestConfig);
            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            log.error("sendHttpPost exception ", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                log.error("close client exception ", e);
            }
        }
        return responseContent;
    }


    public static String sendHttpPut(String httpUrl, String body, Map<String, String> header) {
        HttpPut httpPut = new HttpPut(httpUrl);
        try {
            if (StringUtils.isNotEmpty(body)) {
                StringEntity stringEntity = new StringEntity(body, "UTF-8");
                stringEntity.setContentType("application/json");
                httpPut.setEntity(stringEntity);
            }
            if (CollectionUtil.isNotEmpty(header)) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpPut.addHeader(entry.getKey(), entry.getValue());
                }
            }
        } catch (Exception e) {
            log.error("httpUrl is {}, body is {}, header is {}, error: ", httpUrl, body, header.toString(), e);
        }
        return sendHttpPut(httpPut);
    }

    private static String sendHttpPut(HttpPut httpPut) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String responseContent = "";
        try {
            httpClient = HttpClients.createDefault();
            httpPut.setConfig(requestConfig);
            response = httpClient.execute(httpPut);
            HttpEntity entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            log.error("sendHttpPut exception ", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                log.error("close client exception ", e);
            }
        }
        return responseContent;
    }


    public static String sendHttpGet(String httpUrl, Map<String, String> params, Map<String, String> header) {
        List<BasicNameValuePair> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(params)) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            String paramStr = "";
            try {
                paramStr = EntityUtils.toString(new UrlEncodedFormEntity(list, Consts.UTF_8));
            } catch (IOException e) {
                log.error("params NameValuePair to String exception: ", e);
            }
            if (StringUtils.isNotEmpty(paramStr)) {
                httpUrl = httpUrl + "?" + paramStr;
            }
        }
        HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求
        if (CollectionUtil.isNotEmpty(header)) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                httpGet.addHeader(entry.getKey(), entry.getValue());
            }
        }
        return sendHttpGet(httpGet);
    }

    public static String sendHttpGet(String httpUrl) {
        HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求
        return sendHttpGet(httpGet);
    }

    private static String sendHttpGet(HttpGet httpGet) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        String responseContent = null;
        try {
            httpClient = HttpClients.createDefault();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            log.error("sendHttpGet error, httpUrl={}, error: ", httpGet.getURI().getPath(), e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                log.error("close resources error", e);
            }
        }
        if (StringUtils.isEmpty(responseContent)) {
            log.warn("responseContent is null");
        }
        return responseContent;
    }

    public static String sendHttpDelete(String httpUrl, Map<String, String> header) {
        HttpDelete httpDelete = new HttpDelete(httpUrl);
        if (header != null && !header.isEmpty()) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                httpDelete.addHeader(entry.getKey(), entry.getValue());
            }
        }
        return sendHttpDelete(httpDelete);
    }

    private static String sendHttpDelete(HttpDelete httpDelete) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        String responseContent = null;
        try {
            httpClient = HttpClients.createDefault();
            httpDelete.setConfig(requestConfig);
            response = httpClient.execute(httpDelete);
            entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            log.error("sendHttpDelete error, httpUrl={}, error: ", httpDelete.getURI().getPath(), e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                log.error("close resources error", e);
            }
        }
        if (StringUtils.isEmpty(responseContent)) {
            log.warn("responseContent is null");
        }
        return responseContent;
    }
}

依赖

<dependency>
	<groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.16</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.13</version>
</dependency>
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值