|
@@ -0,0 +1,125 @@
|
|
|
+package com.rtrh.projects.util;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
+// import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
|
+import com.rtrh.core.vo.Message;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+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.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class HttpClientUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送 GET 请求
|
|
|
+ *
|
|
|
+ * @param url 请求地址
|
|
|
+ * @param params 请求参数(键值对形式)
|
|
|
+ * @return 响应结果字符串
|
|
|
+ */
|
|
|
+ public static Object sendGetRequest(String url, Map<String, String> params) {
|
|
|
+ try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
+ // 拼接参数到 URL
|
|
|
+ StringBuilder sb = new StringBuilder(url);
|
|
|
+ if (params != null && !params.isEmpty()) {
|
|
|
+ sb.append("?");
|
|
|
+ for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
+ sb.append(entry.getKey())
|
|
|
+ .append("=")
|
|
|
+ .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString()))
|
|
|
+ .append("&");
|
|
|
+ }
|
|
|
+ sb.deleteCharAt(sb.length() - 1); // 移除最后一个 &
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建 GET 请求
|
|
|
+ HttpGet request = new HttpGet(sb.toString());
|
|
|
+
|
|
|
+ // 发送请求并获取响应
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(request)) {
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ if (entity != null) {
|
|
|
+ String responseBody = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
|
|
+ return JSON.parseObject(responseBody, Message.class).getData();
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送 POST 请求
|
|
|
+ *
|
|
|
+ * @param url 请求地址
|
|
|
+ * @param jsonBody JSON 格式的请求体
|
|
|
+ * @param headers 请求头(键值对形式)
|
|
|
+ * @return 响应结果字符串
|
|
|
+ */
|
|
|
+ public static Object sendPostRequest(String url, Object jsonBody, Map<String, String> headers) {
|
|
|
+ try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
+ // 创建 POST 请求
|
|
|
+ HttpPost request = new HttpPost(url);
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ if (headers != null) {
|
|
|
+ for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
+ request.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ // 注册Java 8日期时间模块
|
|
|
+ // objectMapper.registerModule(new JavaTimeModule());
|
|
|
+ // 禁用时间戳格式,默认改为ISO-8601格式
|
|
|
+ // objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
|
|
+ // 自定义日期格式为数据库兼容格式
|
|
|
+ // objectMapper.setDateFormat(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
|
|
+ String jsonBodyStr = objectMapper.writeValueAsString(jsonBody);
|
|
|
+
|
|
|
+ // 设置请求体
|
|
|
+ if (!jsonBodyStr.isEmpty()) {
|
|
|
+ StringEntity entity = new StringEntity(jsonBodyStr, StandardCharsets.UTF_8);
|
|
|
+ entity.setContentType("application/json");
|
|
|
+ request.setEntity(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送请求并获取响应
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(request)) {
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ if (entity != null) {
|
|
|
+ String responseBody = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
|
|
+ if (!responseBody.isEmpty()){
|
|
|
+ return JSON.parseObject(responseBody, Message.class).getData();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Object sendPostRequest(String url, Object jsonBody) {
|
|
|
+ return sendPostRequest(url, jsonBody, null);
|
|
|
+ }
|
|
|
+}
|