Http请求工具类

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
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 java.io.IOException;
import java.util.Map;

@Slf4j
public class HttpUtils {

   public static String doGet(String url, Map<String, String> requestMap, Map<String, String> headMap) throws IOException {
      if (requestMap != null && requestMap.size() > 0) {
         url += "?";
         StringBuilder sb = new StringBuilder(url);
         for (String key : requestMap.keySet()) {
            sb.append(key).append("=").append(requestMap.get(key)).append("&");
         }
         url = sb.toString();
         url = url.substring(0, url.length() - 1);
      }
      log.info("GET请求,请求地址:[{}],请求头:[{}]", url, JSONObject.toJSONString(headMap));
      HttpGet httpGet = new HttpGet(url);
      return doHttpRequest(httpGet, headMap);
   }


   public static String doPost(String url, String str, Map<String, String> headMap) throws IOException {
      log.info("POST请求,请求地址:[{}],请求参数:[{}],请求头:[{}]", url, str, JSONObject.toJSONString(headMap));
      HttpPost httpPost = new HttpPost(url);
      StringEntity entity = new StringEntity(str, "UTF-8");
      httpPost.setEntity(entity);
      return doHttpRequest(httpPost, headMap);
   }


   public static String doPut(String url, String str, Map<String, String> headMap) throws IOException {
      log.info("PUT请求,请求地址:[{}],请求参数:[{}],请求头:[{}]", url, str, JSONObject.toJSONString(headMap));
      HttpPut httpPut = new HttpPut(url);
      StringEntity entity = new StringEntity(str, "UTF-8");
      httpPut.setEntity(entity);
      return doHttpRequest(httpPut, headMap);
   }


   public static String doDelete(String url, Map<String, String> requestMap, Map<String, String> headMap) throws IOException {
      if (requestMap != null && requestMap.size() > 0) {
         url += "?";
         StringBuilder sb = new StringBuilder(url);
         for (String key : requestMap.keySet()) {
            sb.append(key).append("=").append(requestMap.get(key)).append("&");
         }
         url = sb.toString();
         url = url.substring(0, url.length() - 1);
      }
      log.info("DELETE请求,请求地址:[{}],请求头:[{}]", url, JSONObject.toJSONString(headMap));
      HttpDelete httpDelete = new HttpDelete(url);
      return doHttpRequest(httpDelete, headMap);
   }

   private static String doHttpRequest(HttpRequestBase httpRequest, Map<String, String> headMap) throws IOException {
      httpRequest.setHeader("Content-type", "application/json");
      httpRequest.setHeader("DataEncoding", "UTF-8");
      if (headMap != null) {
         for (String headKey : headMap.keySet()) {
            httpRequest.setHeader(headKey, headMap.get(headKey));
         }
      }
      RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(6000)
            .setConnectionRequestTimeout(6000)
            .setSocketTimeout(6000).build();
      httpRequest.setConfig(requestConfig);
      CloseableHttpResponse httpResponse = null;
      // 设置使用系统代理
      CloseableHttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build();
      try {
         httpResponse = httpClient.execute(httpRequest);
         HttpEntity entity = httpResponse.getEntity();
         return EntityUtils.toString(entity, "UTF-8");
      } finally {
         if (httpResponse != null) {
            try {
               httpResponse.close();
            } catch (IOException e) {
               log.error("");
            }
         }
         if (httpClient != null) {
            try {
               httpClient.close();
            } catch (IOException e) {
               log.error("");
            }
         }
      }
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值