适配get和post参数传递方式
This commit is contained in:
@@ -67,6 +67,7 @@ public class JMeterUtil {
|
||||
String method = jmeterRequest.getMethod().toUpperCase();
|
||||
String requestBody = jmeterRequest.getRequestBody();
|
||||
String requestParams = jmeterRequest.getRequestParams();
|
||||
Map<String, String> requestParamsMap = convertJsonStringToMap(requestParams);
|
||||
String requestHeader = jmeterRequest.getRequestHeader();
|
||||
String jmeterHomePath = jmeterRequest.getJmeterHomePath();
|
||||
Map<String, String> 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<String, String> 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<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;
|
||||
}
|
||||
|
||||
@@ -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<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请求方式
|
||||
* @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<String, String> 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);
|
||||
|
||||
Reference in New Issue
Block a user