HttpClientUtil.java 3.89 KB
package com.ruoyi.utils;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;

/**
 * @author liujun
 * @date 2024年08月06日 16:54
 */
@Slf4j
@Component
public class HttpClientUtil {
    public String doPost(String url, Map<String, Object> paraMap, Map<String, Object> head) {
        HttpClient client = null;
        HttpPost post = null;
        try {
            client = HttpClients.createDefault();
            post = new HttpPost();
            post.setURI(new URI(url));
            if (MapUtils.isNotEmpty(paraMap)) {
//                ArrayList<BasicNameValuePair> list = new ArrayList<>();
//                Set<Map.Entry<String, Object>> entrySet = paraMap.entrySet();
//                for (Map.Entry<String,Object> entry : entrySet) {
//                    String key = entry.getKey();
//                    String value = Objects.isNull(entry.getValue())?"":entry.getValue().toString();
//                    list.add(new BasicNameValuePair(key, value));
//                }
//                post.setEntity(new UrlEncodedFormEntity(list, org.apache.http.protocol.HTTP.UTF_8));
                StringEntity entity = new StringEntity(JSON.toJSONString(paraMap), ContentType.APPLICATION_JSON);
                post.setEntity(entity);
            }

            if (MapUtils.isNotEmpty(head)) {
                for (Map.Entry<String, Object> entry : head.entrySet()) {
                    String value = Objects.isNull(entry.getValue()) ? "" : entry.getValue().toString();
                    post.addHeader(entry.getKey(), value);
                }
            }


            HttpResponse response = client.execute(post);
            if (Objects.isNull(response) || response.getStatusLine().getStatusCode() != 200) {
                log.info("[{}]请求数据错误", url, response.getEntity());
                return null;
            }
          return EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (URISyntaxException e) {
            log.error("请求数据,URL错误,请检查请求URL,[{}]", url, e);
        } catch (ClientProtocolException e) {
            log.error("请求数据错误,[{}]", url, e);
        } catch (IOException e) {
            log.error("请求数据错误,[{}]", url, e);
        }
        return null;
    }


    public static String get(String url) {
        String resultContent = null;
        HttpGet httpGet = new HttpGet(url);
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = httpclient.execute(httpGet)) {

                HttpEntity entity = response.getEntity();
                // 获取响应信息
                resultContent = EntityUtils.toString(entity);
                if(StringUtils.isNotEmpty(resultContent)){
                    resultContent = new String(resultContent.getBytes(),StandardCharsets.UTF_8);
                }
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        return resultContent;
    }
}