HttpUtil

HttpUtil


import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
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.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.util.Map;

@Slf4j
public class HttpClient {
    public HttpClient() {
    }

    private CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    @Autowired
    private RequestConfig config;

    /**
     * 不带参数的get请求,如果状态码为200,则返回body,如果不为200,则返回null
     *
     * @param url 请求url
     * @return 返回结果
     * @throws Exception 异常抛出
     */
    public String doGet(String url) {
        try {
            // 声明 http get 请求
            HttpGet httpGet = new HttpGet(url);
            // 装载配置信息
            httpGet.setConfig(config);
            // 发起请求
            CloseableHttpResponse response = this.httpClient.execute(httpGet);
            if (response != null) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param url 请求url
     * @param map 请求参数
     * @return 返回结果
     * @throws Exception 异常抛出
     */
    public String doGet(String url, Map<String, Object> map) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);
        if (map != null) {
            // 遍历map,拼接请求参数
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
            }
        }
        // 调用不带参数的get请求
        return this.doGet(uriBuilder.build().toString());
    }

    /**
     * 不带参数post请求
     *
     * @param url 请求url
     * @return 返回结果
     * @throws Exception 异常抛出
     */
    public String doPost(String url) throws Exception {
        return this.doPost(url, "");
    }

    /**
     * 带Json参数的Post请求
     *
     * @param url       请求url
     * @param jsonParam json字符串
     * @return 返回结果
     * @throws Exception 异常抛出
     */
    public String doPost(String url, String jsonParam) throws Exception {
        String result = "";
        // 声明httpPost请求
        HttpPost httpPost = new HttpPost(url);
        // 加入配置信息
        httpPost.setConfig(config);
        httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Accept-Encoding", "UTF-8");

        StringEntity s = new StringEntity(jsonParam, "UTF-8");
        s.setContentEncoding("UTF-8");
        s.setContentType("application/json");
        httpPost.setEntity(s);
        HttpResponse response = httpClient.execute(httpPost);
        if (response != null && response.getStatusLine().getStatusCode() == 200) {
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
        }

        return result;
    }

    public InputStream getInputStream(String url, String jsonParam) throws Exception {
        String result = "";
        // 声明httpPost请求
        HttpPost httpPost = new HttpPost(url);
        // 加入配置信息
        httpPost.setConfig(config);
        httpPost.setHeader("Content-Type", "application/json");
        if (jsonParam.length() > 0) {
            httpPost.setEntity(new StringEntity(jsonParam, ContentType.create("application/json", "utf-8")));
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                return entity.getContent();
            }
        }
        return null;
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值