From 861176a0b7c269a6d7cc28939b2030f205dc43a7 Mon Sep 17 00:00:00 2001 From: liangdaliang Date: Sun, 9 Mar 2025 23:38:41 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8Dget=E5=92=8Cpost=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E4=BC=A0=E9=80=92=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/test/common/utils/JMeterUtil.java | 60 ++++++++++++------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/test-common/src/main/java/com/test/common/utils/JMeterUtil.java b/test-common/src/main/java/com/test/common/utils/JMeterUtil.java index de5b8db..16837d7 100644 --- a/test-common/src/main/java/com/test/common/utils/JMeterUtil.java +++ b/test-common/src/main/java/com/test/common/utils/JMeterUtil.java @@ -67,6 +67,7 @@ public class JMeterUtil { String method = jmeterRequest.getMethod().toUpperCase(); String requestBody = jmeterRequest.getRequestBody(); String requestParams = jmeterRequest.getRequestParams(); + Map requestParamsMap = convertJsonStringToMap(requestParams); String requestHeader = jmeterRequest.getRequestHeader(); String jmeterHomePath = jmeterRequest.getJmeterHomePath(); Map result = null; @@ -89,13 +90,13 @@ public class JMeterUtil { url, // 请求地址 port, requestBody, // 请求体 - requestParams // 查询参数 + requestParamsMap // 查询参数 ); } else { httpSampler = createGetHttpSampler( url, // 请求地址 port, - requestParams // 查询参数 + requestParamsMap // 查询参数 ); } // 5. 创建请求头管理器 @@ -206,7 +207,7 @@ public class JMeterUtil { * @return * @throws MalformedURLException */ - private static HTTPSamplerProxy createGetHttpSampler(String urlString, int port, String requestParams) throws MalformedURLException { + private static HTTPSamplerProxy createGetHttpSampler(String urlString, int port, Map requestParams) throws MalformedURLException { URL url = new URL(urlString); // 提取域名(不包括端口号) String domain = url.getHost(); @@ -217,8 +218,9 @@ public class JMeterUtil { httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName()); httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName()); httpSampler.setDomain(domain); // 提取域名 - if (!StringUtils.isEmpty(requestParams)) { - path = path + "?" + requestParams; + if (requestParams != null) { + path += "?"; + path += mapToQueryString(requestParams); } httpSampler.setPath(path); // 提取路径 httpSampler.setPort(port); @@ -226,19 +228,6 @@ public class JMeterUtil { httpSampler.setConnectTimeout("5000"); httpSampler.setUseKeepAlive(true); - // 添加请求参数 -// Map parameters = new HashMap<>(); -// if (!StringUtils.isEmpty(requestJson)) { -// Gson gson = new Gson(); -// // 定义 Map 的类型 -// Type type = new TypeToken>() {}.getType(); -// // 将 JSON 字符串转换为 Map -// parameters = gson.fromJson(requestJson, type); -// path += "?"; -// path += mapToQueryString(parameters); -// httpSampler.setPath(path); // 提取路径 -// } - return httpSampler; } @@ -261,6 +250,34 @@ public class JMeterUtil { return queryString.toString(); } + /** + * 转换[{"key":"authorization","value":"tttt"},{"key":"name","value":"123"}]为Map结构 + * @param jsonString + * @return + */ + private static Map convertJsonStringToMap(String jsonString) { + if (StringUtils.isEmpty(jsonString) || jsonString.equals("[]")) { + return null; + } + Gson gson = new Gson(); + // 定义类型,用于告诉Gson我们期望的结果类型是一个包含键值对的对象列表 + Type listType = new TypeToken>>(){}.getType(); + + // 将JSON字符串转换为List> + List> keyValuePairs = gson.fromJson(jsonString, listType); + + // 创建一个新的Map用于存储结果 + Map resultMap = new HashMap<>(); + for (Map pair : keyValuePairs) { + // 确保每个map都有"key"和"value"两个字段 + if (pair.containsKey("key") && pair.containsKey("value")) { + resultMap.put(pair.get("key"), pair.get("value")); + } + } + + return resultMap; + } + /** * 创建post请求方式 * @param urlString @@ -270,7 +287,7 @@ public class JMeterUtil { * @return * @throws MalformedURLException */ - private static HTTPSamplerProxy createPostHttpSampler(String urlString, int port, String requestBody, String requestParams) throws MalformedURLException { + private static HTTPSamplerProxy createPostHttpSampler(String urlString, int port, String requestBody, Map requestParams) throws MalformedURLException { URL url = new URL(urlString); // 提取域名(不包括端口号) String domain = url.getHost(); @@ -282,8 +299,9 @@ public class JMeterUtil { httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName()); httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName()); httpSampler.setDomain(domain); - if (!StringUtils.isEmpty(requestParams)) { - path = path + "?" + requestParams; + if (requestParams != null) { + path += "?"; + path += mapToQueryString(requestParams); } httpSampler.setPath(path); httpSampler.setPort(port);