From 73bab98fa6620a7648927fbab9856dd7cb334726 Mon Sep 17 00:00:00 2001
From: Administrator <zhubaomin>
Date: 星期三, 27 十二月 2023 17:38:19 +0800
Subject: [PATCH] 2023-12-27 朱宝民 完善了几个功能
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HttpUtils.java | 153 +++++++++++++++++++++++++-------------------------
1 files changed, 76 insertions(+), 77 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HttpUtils.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HttpUtils.java
index 633f940..04bd793 100644
--- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HttpUtils.java
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HttpUtils.java
@@ -1,7 +1,7 @@
package com.dy.common.util;
-import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
@@ -36,18 +36,18 @@
/**
* get
*
- * @param host
- * @param path
- * @param headers
- * @param querys
- * @return
- * @throws Exception
+ * @param host 鏈嶅姟绔疷RI
+ * @param path 璇锋眰璺緞
+ * @param headers 璇锋眰澶�
+ * @param params 璇锋眰鍙傛暟
+ * @return HttpResponse鍝嶅簲
+ * @throws Exception 寮傚父
*/
public static HttpResponse doGet(String host, String path, Map<String, String> headers,
- Map<String, String> querys) throws Exception {
+ Map<String, String> params) throws Exception{
HttpClient httpClient = wrapClient(host);
- HttpGet request = new HttpGet(buildUrl(host, path, querys));
+ HttpGet request = new HttpGet(buildUrl(host, path, params));
if(headers != null){
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
@@ -59,29 +59,29 @@
/**
* post form
- *
- * @param host
- * @param path
- * @param headers
- * @param querys
- * @param bodys
- * @return
- * @throws Exception
+ *
+ * @param host 鏈嶅姟绔疷RI
+ * @param path 璇锋眰璺緞
+ * @param headers 璇锋眰澶�
+ * @param params 璇锋眰鍙傛暟
+ * @param bodies 璇锋眰浣�
+ * @return HttpResponse鍝嶅簲
+ * @throws Exception 寮傚父
*/
public static HttpResponse doPost(String host, String path, Map<String, String> headers,
- Map<String, String> querys, Map<String, String> bodys) throws Exception {
+ Map<String, String> params, Map<String, String> bodies) throws Exception {
HttpClient httpClient = wrapClient(host);
- HttpPost request = new HttpPost(buildUrl(host, path, querys));
+ HttpPost request = new HttpPost(buildUrl(host, path, params));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
- if (bodys != null) {
- List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
+ if (bodies != null) {
+ List<NameValuePair> nameValuePairList = new ArrayList<>();
- for (String key : bodys.keySet()) {
- nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
+ for (String key : bodies.keySet()) {
+ nameValuePairList.add(new BasicNameValuePair(key, bodies.get(key)));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
@@ -93,20 +93,20 @@
/**
* Post String
- *
- * @param host
- * @param path
- * @param headers
- * @param querys
- * @param body
- * @return
- * @throws Exception
+ *
+ * @param host 鏈嶅姟绔疷RI
+ * @param path 璇锋眰璺緞
+ * @param headers 璇锋眰澶�
+ * @param params 璇锋眰鍙傛暟
+ * @param body 璇锋眰浣�
+ * @return HttpResponse鍝嶅簲
+ * @throws Exception 寮傚父
*/
public static HttpResponse doPost(String host, String path, Map<String, String> headers,
- Map<String, String> querys, String body) throws Exception {
+ Map<String, String> params, String body) throws Exception {
HttpClient httpClient = wrapClient(host);
- HttpPost request = new HttpPost(buildUrl(host, path, querys));
+ HttpPost request = new HttpPost(buildUrl(host, path, params));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
@@ -120,20 +120,20 @@
/**
* Post stream
- *
- * @param host
- * @param path
- * @param headers
- * @param querys
- * @param body
- * @return
- * @throws Exception
+ *
+ * @param host 鏈嶅姟绔疷RI
+ * @param path 璇锋眰璺緞
+ * @param headers 璇锋眰澶�
+ * @param params 璇锋眰鍙傛暟
+ * @param body 璇锋眰浣�
+ * @return HttpResponse鍝嶅簲
+ * @throws Exception 寮傚父
*/
public static HttpResponse doPost(String host, String path, Map<String, String> headers,
- Map<String, String> querys, byte[] body) throws Exception {
+ Map<String, String> params, byte[] body) throws Exception {
HttpClient httpClient = wrapClient(host);
- HttpPost request = new HttpPost(buildUrl(host, path, querys));
+ HttpPost request = new HttpPost(buildUrl(host, path, params));
if(headers != null){
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
@@ -149,20 +149,20 @@
/**
* Put String
- *
- * @param host
- * @param path
- * @param headers
- * @param querys
- * @param body
- * @return
- * @throws Exception
+ *
+ * @param host 鏈嶅姟绔疷RI
+ * @param path 璇锋眰璺緞
+ * @param headers 璇锋眰澶�
+ * @param params 璇锋眰鍙傛暟
+ * @param body 璇锋眰浣�
+ * @return HttpResponse鍝嶅簲
+ * @throws Exception 寮傚父
*/
public static HttpResponse doPut(String host, String path, Map<String, String> headers,
- Map<String, String> querys, String body) throws Exception {
+ Map<String, String> params, String body) throws Exception {
HttpClient httpClient = wrapClient(host);
- HttpPut request = new HttpPut(buildUrl(host, path, querys));
+ HttpPut request = new HttpPut(buildUrl(host, path, params));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
@@ -176,20 +176,20 @@
/**
* Put stream
- *
- * @param host
- * @param path
- * @param headers
- * @param querys
- * @param body
- * @return
- * @throws Exception
+ *
+ * @param host 鏈嶅姟绔疷RI
+ * @param path 璇锋眰璺緞
+ * @param headers 璇锋眰澶�
+ * @param params 璇锋眰鍙傛暟
+ * @param body 璇锋眰浣�
+ * @return HttpResponse鍝嶅簲
+ * @throws Exception 寮傚父
*/
public static HttpResponse doPut(String host, String path, Map<String, String> headers,
- Map<String, String> querys, byte[] body) throws Exception {
+ Map<String, String> params, byte[] body) throws Exception {
HttpClient httpClient = wrapClient(host);
- HttpPut request = new HttpPut(buildUrl(host, path, querys));
+ HttpPut request = new HttpPut(buildUrl(host, path, params));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
@@ -203,19 +203,19 @@
/**
* Delete
- *
- * @param host
- * @param path
- * @param headers
- * @param querys
- * @return
- * @throws Exception
+ *
+ * @param host 鏈嶅姟绔疷RI
+ * @param path 璇锋眰璺緞
+ * @param headers 璇锋眰澶�
+ * @param params 璇锋眰鍙傛暟
+ * @return HttpResponse鍝嶅簲
+ * @throws Exception 寮傚父
*/
public static HttpResponse doDelete(String host, String path, Map<String, String> headers,
- Map<String, String> querys) throws Exception {
+ Map<String, String> params) throws Exception {
HttpClient httpClient = wrapClient(host);
- HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
+ HttpDelete request = new HttpDelete(buildUrl(host, path, params));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
@@ -223,16 +223,15 @@
return httpClient.execute(request);
}
- private static String buildUrl(String host, String path, Map<String, String> querys)
- throws UnsupportedEncodingException {
+ private static String buildUrl(String host, String path, Map<String, String> params) {
StringBuilder sbUrl = new StringBuilder();
sbUrl.append(host);
if (!StringUtils.isBlank(path)) {
sbUrl.append(path);
}
- if (null != querys) {
+ if (null != params) {
StringBuilder sbQuery = new StringBuilder();
- for (Map.Entry<String, String> query : querys.entrySet()) {
+ for (Map.Entry<String, String> query : params.entrySet()) {
if (0 < sbQuery.length()) {
sbQuery.append("&");
}
@@ -243,7 +242,7 @@
sbQuery.append(query.getKey());
if (!StringUtils.isBlank(query.getValue())) {
sbQuery.append("=");
- sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
+ sbQuery.append(URLEncoder.encode(query.getValue(), StandardCharsets.UTF_8));
}
}
}
--
Gitblit v1.8.0