适配get和post参数传递方式
This commit is contained in:
@@ -67,6 +67,7 @@ public class JMeterUtil {
|
|||||||
String method = jmeterRequest.getMethod().toUpperCase();
|
String method = jmeterRequest.getMethod().toUpperCase();
|
||||||
String requestBody = jmeterRequest.getRequestBody();
|
String requestBody = jmeterRequest.getRequestBody();
|
||||||
String requestParams = jmeterRequest.getRequestParams();
|
String requestParams = jmeterRequest.getRequestParams();
|
||||||
|
Map<String, String> requestParamsMap = convertJsonStringToMap(requestParams);
|
||||||
String requestHeader = jmeterRequest.getRequestHeader();
|
String requestHeader = jmeterRequest.getRequestHeader();
|
||||||
String jmeterHomePath = jmeterRequest.getJmeterHomePath();
|
String jmeterHomePath = jmeterRequest.getJmeterHomePath();
|
||||||
Map<String, String> result = null;
|
Map<String, String> result = null;
|
||||||
@@ -89,13 +90,13 @@ public class JMeterUtil {
|
|||||||
url, // 请求地址
|
url, // 请求地址
|
||||||
port,
|
port,
|
||||||
requestBody, // 请求体
|
requestBody, // 请求体
|
||||||
requestParams // 查询参数
|
requestParamsMap // 查询参数
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
httpSampler = createGetHttpSampler(
|
httpSampler = createGetHttpSampler(
|
||||||
url, // 请求地址
|
url, // 请求地址
|
||||||
port,
|
port,
|
||||||
requestParams // 查询参数
|
requestParamsMap // 查询参数
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// 5. 创建请求头管理器
|
// 5. 创建请求头管理器
|
||||||
@@ -206,7 +207,7 @@ public class JMeterUtil {
|
|||||||
* @return
|
* @return
|
||||||
* @throws MalformedURLException
|
* @throws MalformedURLException
|
||||||
*/
|
*/
|
||||||
private static HTTPSamplerProxy createGetHttpSampler(String urlString, int port, String requestParams) throws MalformedURLException {
|
private static HTTPSamplerProxy createGetHttpSampler(String urlString, int port, Map<String, String> requestParams) throws MalformedURLException {
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
// 提取域名(不包括端口号)
|
// 提取域名(不包括端口号)
|
||||||
String domain = url.getHost();
|
String domain = url.getHost();
|
||||||
@@ -217,8 +218,9 @@ public class JMeterUtil {
|
|||||||
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
|
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
|
||||||
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
|
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
|
||||||
httpSampler.setDomain(domain); // 提取域名
|
httpSampler.setDomain(domain); // 提取域名
|
||||||
if (!StringUtils.isEmpty(requestParams)) {
|
if (requestParams != null) {
|
||||||
path = path + "?" + requestParams;
|
path += "?";
|
||||||
|
path += mapToQueryString(requestParams);
|
||||||
}
|
}
|
||||||
httpSampler.setPath(path); // 提取路径
|
httpSampler.setPath(path); // 提取路径
|
||||||
httpSampler.setPort(port);
|
httpSampler.setPort(port);
|
||||||
@@ -226,19 +228,6 @@ public class JMeterUtil {
|
|||||||
httpSampler.setConnectTimeout("5000");
|
httpSampler.setConnectTimeout("5000");
|
||||||
httpSampler.setUseKeepAlive(true);
|
httpSampler.setUseKeepAlive(true);
|
||||||
|
|
||||||
// 添加请求参数
|
|
||||||
// Map<String, String> parameters = new HashMap<>();
|
|
||||||
// if (!StringUtils.isEmpty(requestJson)) {
|
|
||||||
// Gson gson = new Gson();
|
|
||||||
// // 定义 Map 的类型
|
|
||||||
// Type type = new TypeToken<Map<String, String>>() {}.getType();
|
|
||||||
// // 将 JSON 字符串转换为 Map
|
|
||||||
// parameters = gson.fromJson(requestJson, type);
|
|
||||||
// path += "?";
|
|
||||||
// path += mapToQueryString(parameters);
|
|
||||||
// httpSampler.setPath(path); // 提取路径
|
|
||||||
// }
|
|
||||||
|
|
||||||
return httpSampler;
|
return httpSampler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,6 +250,34 @@ public class JMeterUtil {
|
|||||||
return queryString.toString();
|
return queryString.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换[{"key":"authorization","value":"tttt"},{"key":"name","value":"123"}]为Map结构
|
||||||
|
* @param jsonString
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static Map<String, String> convertJsonStringToMap(String jsonString) {
|
||||||
|
if (StringUtils.isEmpty(jsonString) || jsonString.equals("[]")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Gson gson = new Gson();
|
||||||
|
// 定义类型,用于告诉Gson我们期望的结果类型是一个包含键值对的对象列表
|
||||||
|
Type listType = new TypeToken<List<Map<String, String>>>(){}.getType();
|
||||||
|
|
||||||
|
// 将JSON字符串转换为List<Map<String, String>>
|
||||||
|
List<Map<String, String>> keyValuePairs = gson.fromJson(jsonString, listType);
|
||||||
|
|
||||||
|
// 创建一个新的Map<String, String>用于存储结果
|
||||||
|
Map<String, String> resultMap = new HashMap<>();
|
||||||
|
for (Map<String, String> pair : keyValuePairs) {
|
||||||
|
// 确保每个map都有"key"和"value"两个字段
|
||||||
|
if (pair.containsKey("key") && pair.containsKey("value")) {
|
||||||
|
resultMap.put(pair.get("key"), pair.get("value"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultMap;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建post请求方式
|
* 创建post请求方式
|
||||||
* @param urlString
|
* @param urlString
|
||||||
@@ -270,7 +287,7 @@ public class JMeterUtil {
|
|||||||
* @return
|
* @return
|
||||||
* @throws MalformedURLException
|
* @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<String, String> requestParams) throws MalformedURLException {
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
// 提取域名(不包括端口号)
|
// 提取域名(不包括端口号)
|
||||||
String domain = url.getHost();
|
String domain = url.getHost();
|
||||||
@@ -282,8 +299,9 @@ public class JMeterUtil {
|
|||||||
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
|
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
|
||||||
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
|
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
|
||||||
httpSampler.setDomain(domain);
|
httpSampler.setDomain(domain);
|
||||||
if (!StringUtils.isEmpty(requestParams)) {
|
if (requestParams != null) {
|
||||||
path = path + "?" + requestParams;
|
path += "?";
|
||||||
|
path += mapToQueryString(requestParams);
|
||||||
}
|
}
|
||||||
httpSampler.setPath(path);
|
httpSampler.setPath(path);
|
||||||
httpSampler.setPort(port);
|
httpSampler.setPort(port);
|
||||||
|
|||||||
Reference in New Issue
Block a user