jmeter-beanshell-procesor
前面提到的提取前提条件:在响应报文中提取,
现在条件为:在请求报文里
假设我的请求报文是{
"username": "admin",
"password": "123456"
}代码如下
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
// 获取输入请求数据
String request = prev.getSamplerData();
// 使用 log.error 打印请求字符串,用于检查
log.error("Request: " + request);
// 定义新的正则表达式模式来提取 username 和 password
Pattern patternUsername = Pattern.compile("\"username\":\\s*\"([^\"]+)\"");
Pattern patternPassword = Pattern.compile("\"password\":\\s*\"([^\"]+)\"");
Matcher matcherUsername = patternUsername.matcher(request);
Matcher matcherPassword = patternPassword.matcher(request);
String username = "";
String password = "";
// 提取 username
if (matcherUsername.find()) {
username = matcherUsername.group(1);
} else {
log.error("Failed to extract username from the request.");
}
// 提取 password
if (matcherPassword.find()) {
password = matcherPassword.group(1);
} else {
log.error("Failed to extract password from the request.");
}
// 创建或确保 D 盘的 log 文件夹存在
File logFolder = new File("D:\\log");
if (!logFolder.exists()) {
logFolder.mkdirs();
}
// 写入文件
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\log\\log.txt", true));
writer.write(username + "," + password + "\n");
writer.close();
} catch (IOException e) {
log.error("Error occurred while writing to the file: " + e.getMessage());
}
标签:username,jmeter,提取,log,java,报文,request,import,password
From: https://www.cnblogs.com/yimouz-219/p/18262845