From c560f1a14ae1fc468d90e50e6a32d805295875da Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期一, 01 九月 2025 15:35:59 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/2.0' into 2.0
---
cloud-server-other/src/main/java/com/dsh/other/util/HttpRequestUtil.java | 461 ++++++++++++++++++++++++++++-----------------------------
1 files changed, 227 insertions(+), 234 deletions(-)
diff --git a/cloud-server-other/src/main/java/com/dsh/other/util/HttpRequestUtil.java b/cloud-server-other/src/main/java/com/dsh/other/util/HttpRequestUtil.java
index 0f47e39..7aee9a0 100644
--- a/cloud-server-other/src/main/java/com/dsh/other/util/HttpRequestUtil.java
+++ b/cloud-server-other/src/main/java/com/dsh/other/util/HttpRequestUtil.java
@@ -15,239 +15,232 @@
import java.util.Map;
public class HttpRequestUtil {
-
- public static String postRequest(String url, Map<String, String> params) {
- // 构造HttpClient的实例
- HttpClient httpClient = new HttpClient();
- // 创建POST方法的实例
- PostMethod postMethod = new PostMethod(url);
- // 设置请求头信息
- postMethod.setRequestHeader("Connection", "close");
- postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
- // 添加参数
- for (Map.Entry<String, String> entry : params.entrySet()) {
- postMethod.addParameter(entry.getKey(), entry.getValue());
- }
- // 使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
- 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 postRequest1(String url, Map<String, String> params,String appKey,String appSecret) {
- // 构造HttpClient的实例
- HttpClient httpClient = new HttpClient();
- // 创建POST方法的实例
- PostMethod postMethod = new PostMethod(url);
- // 设置请求头信息
- String nonce = String.valueOf(Double.valueOf(Math.random() * 1000000.0D).intValue());
- String timeMillis = String.valueOf(System.currentTimeMillis());
- String signature = string2Sha1(appSecret + nonce + timeMillis);
- postMethod.setRequestHeader("Host", "api-cn.ronghub.com");
- postMethod.setRequestHeader("App-Key", appKey);
- postMethod.setRequestHeader("Signature", signature);
- postMethod.setRequestHeader("Nonce", nonce);
- postMethod.setRequestHeader("Timestamp", timeMillis);
- postMethod.setRequestHeader("Host", "api-cn.ronghub.com");
- postMethod.addRequestHeader("Content-Type", "application/json");
- // 添加参数
- for (Map.Entry<String, String> entry : params.entrySet()) {
- postMethod.addParameter(entry.getKey(), entry.getValue());
- }
- // 使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
- 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;
- }
- private static String string2Sha1(String str){
- if(str==null||str.length()==0){
- return null;
- }
- char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
- 'a','b','c','d','e','f'};
- try {
- MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
- mdTemp.update(str.getBytes("UTF-8"));
- byte[] md = mdTemp.digest();
- int j = md.length;
- char buf[] = new char[j*2];
- int k = 0;
- for (int i = 0; i < j; i++) {
- byte byte0 = md[i];
- buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
- buf[k++] = hexDigits[byte0 & 0xf];
- }
- return new String(buf);
- } catch (Exception e) {
- return null;
- }
- }
- public static String getRequest(String url, Map<String, String> params) {
- // 构造HttpClient实例
- HttpClient client = new HttpClient();
- // 拼接参数
- String paramStr = "";
- for (String key : params.keySet()) {
- paramStr = paramStr + "&" + key + "=" + params.get(key);
- }
- paramStr = paramStr.substring(1);
- // 创建GET方法的实例
- GetMethod method = new GetMethod(url + "?" + paramStr);
- // 接收返回结果
- String result = null;
- try {
- // 执行HTTP GET方法请求
- client.executeMethod(method);
- // 返回处理结果
- result = method.getResponseBodyAsString();
- } catch (HttpException e) {
- // 发生致命的异常,可能是协议不对或者返回的内容有问题
- System.out.println("请检查输入的URL!");
- e.printStackTrace();
- } catch (IOException e) {
- // 发生网络异常
- System.out.println("发生网络异常!");
- e.printStackTrace();
- } finally {
- // 释放链接
- method.releaseConnection();
- // 关闭HttpClient实例
- if (client != null) {
- ((SimpleHttpConnectionManager) client
- .getHttpConnectionManager()).shutdown();
- client = null;
- }
- }
- return result;
- }
-
-
- /**
- * 发送网络请求
- * @param url
- * @return
- */
- public static String sendNetRequest(String url, Map<String, String> params) {
- // 构造HttpClient实例
- HttpClient client = new HttpClient();
- String paramStr = "";
- for (String key : params.keySet()) {
- paramStr = paramStr + "&" + key + "=" + params.get(key);
- }
- paramStr = paramStr.substring(1);
- System.err.println(url + "?" + paramStr);
- // 创建GET方法的实例
- GetMethod method = new GetMethod(url + "?" + paramStr);
- // 接收返回结果
- String result = null;
- try {
- // 执行HTTP GET方法请求
- client.executeMethod(method);
- // 返回处理结果
- result = method.getResponseBodyAsString();
- } catch (HttpException e) {
- // 发生致命的异常,可能是协议不对或者返回的内容有问题
- System.out.println("请检查输入的URL!");
- e.printStackTrace();
- } catch (IOException e) {
- // 发生网络异常
- System.out.println("发生网络异常!");
- e.printStackTrace();
- } finally {
- // 释放链接
- method.releaseConnection();
- // 关闭HttpClient实例
- if (client != null) {
- ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
- client = null;
- }
- }
- return result;
- }
-
-
-
- /**
- * jsonp跨域请求数据响应<br/>
- * 方法名:responsejsonpData<br/>
- * @author:Mryang<br/>
- * @createTime:2016年7月31日-下午11:17:31 <br/>
- * @tel: 15198268054<br/>
- * @param request
- * @param response
- * @param map void<br/>
- * @exception <br/>
- * @since 1.0.0
- */
- public void responsejsonpData(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
- response.setCharacterEncoding("UTF-8");
- response.setHeader("Content-Type", "text/html;Charset=utf-8");
- try {
- PrintWriter writer = response.getWriter();
- String params = request.getParameter("callback");
- String json = JSONObject.toJSONString(map);
- writer.print(params+ "("+json+")");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
+ public static String postRequest(String url, Map<String, String> params) {
+ // 构造HttpClient的实例
+ HttpClient httpClient = new HttpClient();
+ // 创建POST方法的实例
+ PostMethod postMethod = new PostMethod(url);
+ // 设置请求头信息
+ postMethod.setRequestHeader("Connection", "close");
+ postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
+ // 添加参数
+ for (Map.Entry<String, String> entry : params.entrySet()) {
+ postMethod.addParameter(entry.getKey(), entry.getValue());
+ }
+ // 使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
+ 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 postRequest1(String url, Map<String, String> params, String appKey, String appSecret) {
+ // 构造HttpClient的实例
+ HttpClient httpClient = new HttpClient();
+ // 创建POST方法的实例
+ PostMethod postMethod = new PostMethod(url);
+ // 设置请求头信息
+ String nonce = String.valueOf(Double.valueOf(Math.random() * 1000000.0D).intValue());
+ String timeMillis = String.valueOf(System.currentTimeMillis());
+ String signature = string2Sha1(appSecret + nonce + timeMillis);
+ postMethod.setRequestHeader("Host", "api-cn.ronghub.com");
+ postMethod.setRequestHeader("App-Key", appKey);
+ postMethod.setRequestHeader("Signature", signature);
+ postMethod.setRequestHeader("Nonce", nonce);
+ postMethod.setRequestHeader("Timestamp", timeMillis);
+ postMethod.setRequestHeader("Host", "api-cn.ronghub.com");
+ postMethod.addRequestHeader("Content-Type", "application/json");
+ // 添加参数
+ for (Map.Entry<String, String> entry : params.entrySet()) {
+ postMethod.addParameter(entry.getKey(), entry.getValue());
+ }
+ // 使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
+ 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;
+ }
+
+ private static String string2Sha1(String str) {
+ if (str == null || str.length() == 0) {
+ return null;
+ }
+ char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ 'a', 'b', 'c', 'd', 'e', 'f'};
+ try {
+ MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
+ mdTemp.update(str.getBytes("UTF-8"));
+
+ byte[] md = mdTemp.digest();
+ int j = md.length;
+ char buf[] = new char[j * 2];
+ int k = 0;
+ for (int i = 0; i < j; i++) {
+ byte byte0 = md[i];
+ buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
+ buf[k++] = hexDigits[byte0 & 0xf];
+ }
+ return new String(buf);
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ public static String getRequest(String url, Map<String, String> params) {
+ // 构造HttpClient实例
+ HttpClient client = new HttpClient();
+ // 拼接参数
+ String paramStr = "";
+ for (String key : params.keySet()) {
+ paramStr = paramStr + "&" + key + "=" + params.get(key);
+ }
+ paramStr = paramStr.substring(1);
+ // 创建GET方法的实例
+ GetMethod method = new GetMethod(url + "?" + paramStr);
+ // 接收返回结果
+ String result = null;
+ try {
+ // 执行HTTP GET方法请求
+ client.executeMethod(method);
+ // 返回处理结果
+ result = method.getResponseBodyAsString();
+ } catch (HttpException e) {
+ // 发生致命的异常,可能是协议不对或者返回的内容有问题
+ System.out.println("请检查输入的URL!");
+ e.printStackTrace();
+ } catch (IOException e) {
+ // 发生网络异常
+ System.out.println("发生网络异常!");
+ e.printStackTrace();
+ } finally {
+ // 释放链接
+ method.releaseConnection();
+ // 关闭HttpClient实例
+ if (client != null) {
+ ((SimpleHttpConnectionManager) client
+ .getHttpConnectionManager()).shutdown();
+ client = null;
+ }
+ }
+ return result;
+ }
+
+
+ /**
+ * 发送网络请求
+ *
+ * @param url
+ * @return
+ */
+ public static String sendNetRequest(String url, Map<String, String> params) {
+ // 构造HttpClient实例
+ HttpClient client = new HttpClient();
+ String paramStr = "";
+ for (String key : params.keySet()) {
+ paramStr = paramStr + "&" + key + "=" + params.get(key);
+ }
+ paramStr = paramStr.substring(1);
+ System.err.println(url + "?" + paramStr);
+ // 创建GET方法的实例
+ GetMethod method = new GetMethod(url + "?" + paramStr);
+ // 接收返回结果
+ String result = null;
+ try {
+ // 执行HTTP GET方法请求
+ client.executeMethod(method);
+ // 返回处理结果
+ result = method.getResponseBodyAsString();
+ } catch (HttpException e) {
+ // 发生致命的异常,可能是协议不对或者返回的内容有问题
+ System.out.println("请检查输入的URL!");
+ e.printStackTrace();
+ } catch (IOException e) {
+ // 发生网络异常
+ System.out.println("发生网络异常!");
+ e.printStackTrace();
+ } finally {
+ // 释放链接
+ method.releaseConnection();
+ // 关闭HttpClient实例
+ if (client != null) {
+ ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
+ client = null;
+ }
+ }
+ return result;
+ }
+
+
+ /**
+ * jsonp跨域请求数据响应<br/>
+ * 方法名:responsejsonpData<br/>
+ *
+ * @param request
+ * @param response
+ * @param map void<br/>
+ * @throws <br/>
+ * @author:Mryang<br/>
+ * @createTime:2016年7月31日-下午11:17:31 <br/>
+ * @tel: 15198268054<br />
+ * @since 1.0.0
+ */
+ public void responsejsonpData(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
+ response.setCharacterEncoding("UTF-8");
+ response.setHeader("Content-Type", "text/html;Charset=utf-8");
+ try {
+ PrintWriter writer = response.getWriter();
+ String params = request.getParameter("callback");
+ String json = JSONObject.toJSONString(map);
+ writer.print(params + "(" + json + ")");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+
}
--
Gitblit v1.7.1