From 9486766c806fe1d9e082b2fd02ea1cc558f1b443 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期四, 08 五月 2025 09:21:57 +0800
Subject: [PATCH] bug修改

---
 cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/HttpRequestUtil.java |  118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 116 insertions(+), 2 deletions(-)

diff --git a/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/HttpRequestUtil.java b/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/HttpRequestUtil.java
index 79d78e4..5adbfaa 100644
--- a/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/HttpRequestUtil.java
+++ b/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/HttpRequestUtil.java
@@ -2,6 +2,8 @@
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.net.URISyntaxException;
+import java.util.Iterator;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
@@ -14,8 +16,78 @@
 import org.apache.commons.httpclient.SimpleHttpConnectionManager;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+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.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+
 public class HttpRequestUtil {
-	
+
+	/***
+	 * get请求(带参数)
+	 * @param url
+	 * @return String
+	 */
+	public static String getReq(String url, Map<String, String> params) {
+		String result = null;
+		try {
+			URIBuilder uriBuilder = new URIBuilder(url);
+			Iterator maplist = params.entrySet().iterator();
+			while (maplist.hasNext()) {
+				Map.Entry<String, String> map = (Map.Entry<String, String>) maplist.next();
+				uriBuilder.addParameter(map.getKey(), map.getValue());
+			}
+			CloseableHttpClient client = HttpClientBuilder.create().build();
+			HttpPost get = new HttpPost(uriBuilder.build());
+			get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
+			HttpResponse response = client.execute(get);
+			result = EntityUtils.toString(response.getEntity(), "UTF-8");
+
+		} catch (URISyntaxException e) {
+			e.printStackTrace();
+		} catch (ClientProtocolException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		   }
+		return result;
+	}/***
+	 * get请求(带参数)
+	 * @param url
+	 * @return String
+	 */
+	public static String getReq1(String url, Map<String, String> params) {
+		String result = null;
+		try {
+			URIBuilder uriBuilder = new URIBuilder(url);
+			Iterator maplist = params.entrySet().iterator();
+			while (maplist.hasNext()) {
+				Map.Entry<String, String> map = (Map.Entry<String, String>) maplist.next();
+				uriBuilder.addParameter(map.getKey(), map.getValue());
+			}
+			CloseableHttpClient client = HttpClientBuilder.create().build();
+			HttpPost get = new HttpPost(uriBuilder.build());
+			// todo 验签
+			get.setHeader("Authorization", "close");
+			get.setHeader("Content-Type", "application/json");
+			get.setHeader("Accept", "application/json");
+			get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
+			HttpResponse response = client.execute(get);
+			result = EntityUtils.toString(response.getEntity(), "UTF-8");
+
+		} catch (URISyntaxException e) {
+			e.printStackTrace();
+		} catch (ClientProtocolException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		   }
+		return result;
+	}
 	public static String postRequest(String url, Map<String, String> params) {
 		// 构造HttpClient的实例
 		HttpClient httpClient = new HttpClient();
@@ -56,7 +128,49 @@
 		}
 		return result;
 	}
-
+	public static String postRequest1(String url, Map<String, Object> params) {
+		// 构造HttpClient的实例
+		HttpClient httpClient = new HttpClient();
+		// 创建POST方法的实例
+		PostMethod postMethod = new PostMethod(url);
+		// 设置请求头信息
+		postMethod.setRequestHeader("Authorization", "close");
+		postMethod.setRequestHeader("Accept", "application/json");
+		postMethod.addRequestHeader("Content-Type", "application/json");
+		postMethod.addRequestHeader("Wechatpay-Serial", "application/json");
+		postMethod.setRequestHeader("Connection", "close");
+		// 添加参数
+		for (Map.Entry<String, Object> entry : params.entrySet()) {
+			postMethod.addParameter(entry.getKey(), entry.getValue().toString());
+		}
+		// 使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
+		httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
+		// 接收处理结果
+		String result = null;
+		try {
+			// 执行Http Post请求
+			httpClient.executeMethod(postMethod);
+			// 返回处理结果
+			result = postMethod.getResponseBodyAsString();
+		} catch (HttpException e) {
+			// 发生致命的异常,可能是协议不对或者返回的内容有问题
+			System.out.println("请检查输入的URL!");
+			e.printStackTrace();
+		} catch (IOException e) {
+			// 发生网络异常
+			System.out.println("发生网络异常!");
+			e.printStackTrace();
+		} finally {
+			// 释放链接
+			postMethod.releaseConnection();
+			// 关闭HttpClient实例
+			if (httpClient != null) {
+				((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
+				httpClient = null;
+			}
+		}
+		return result;
+	}
 	public static String getRequest(String url, Map<String, String> params) {
 		// 构造HttpClient实例
 		HttpClient client = new HttpClient();

--
Gitblit v1.7.1