|
@@ -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.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()) {
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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()) {
|
|
|
+
|
|
|
+ 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();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|