上传图片太麻烦,具体操作见:http://blog.csdn.net/quiet_girl/article/details/50577324
我大概介绍下别人没有的。
1、对请求报文的预处理,beanshell preprocessor的用法。在beanshell preprocessor页面,script脚本处,加入你需要对采样的报文的一些处理。
记得引入jar包,点击测试计划,在本页面,点击添加目录或者jar 到classpath。本实例需要的jar包已上传到我的下载资源了
如果缺少jar宝,会报异常,
org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval ''import ....
在beanshell preprocessor页面,script脚本和如下:
其中com.util.SignUtils该类为自己在eclipse下写的java工具类,用于对请求报文参数所做的一些加密算法处理,并将加密后的密文,添加到请求报文中,再发接口。
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;
import com.util.SignUtils;
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.samplers.*;
import org.apache.jmeter.config.*;
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.protocol.http.util.*;
import java.util.HashMap;
import java.util.Map;
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
try {
//自定义变量
//读取配置.设置头信息和公共信息
//请求体重置加密
Arguments arguments = sampler.getArguments();
Map inMap=new HashMap();
// Argument arg = arguments.getArgument(0);
int len=arguments.getArgumentCount();
for(int i=0;i//设置新post 数据
} } catch (Exception ex) { log.info("Script execution failed===========================================", ex);}
com.util.SignUtils
自定义工具类如下:
public class SignUtils {
public static String getSigeStr(Map paramMap){
String encodeAture="";
encodeAture = MD5Encoder(
paramMap
)
return encode;}
}
另外一个实例:
请求报文的前置处理脚本:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.xx.x.util.*;
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.samplers.*;
import org.apache.jmeter.config.*;
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.protocol.http.util.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import com.doctor.commons.*;
try {
//自定义变量
//读取配置.设置头信息和公共信息
Properties properties = new Properties();
String fileName = ${configFile}+"";
FileInputStream inputStream = new FileInputStream(fileName);
properties.load(inputStream);
String accountId = properties.getProperty("accountId");
String sign = properties.getProperty("sign");
String accessKey = properties.getProperty("accessKey");
String token = properties.getProperty("token");
vars.put("accountId",accountId);
vars.put("sign",sign);
vars.put("accessKey",accessKey);
vars.put("token",token);
if (inputStream != null) {
inputStream.close();
}
String deviceId = ${deviceId}+"";
String secretKey = ${secretKey}+"";
//请求体重置加密
Arguments arguments = sampler.getArguments();
Argument arg = arguments.getArgument(0);
String body = arg.getValue();
log.info("PreProcessor==========================================="+ body);
JSONObject parseObject = JSON.parseObject(body);
log.info("====********************************************************************"+ parseObject);
String data = parseObject.getString("data");
if(data !=null){
log.info("====********************************************************************"+ data);
//加密
String encryptData = AESUtis.appAESEncrypt(data, deviceId, secretKey);
log.info("encryptData====********************************************************************"+ encryptData);
JSONObject jsonObject = new JSONObject();
jsonObject.put("data", encryptData);
String postData = jsonObject.toJSONString();
log.info("postData====**************************************postData******************************"+ postData);
//设置新post 数据
arg.setValue(postData);
}
} catch (Exception ex) {
log.info("Script execution failed===========================================", ex);
}
响应报文的后置处理脚本:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.customerdefine.util.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import com.doctor.commons.*;
try {
//先取自定义变量,等用到的时候,再取说找不到定义
String secretKey = ${secretKey}+"";
String deviceId = ${deviceId}+"";
//服务器返回的公共配置属性保存
String fileName = ${configFile}+"";
log.info("====*fileName*******************************************************************"+ fileName);
//对返回的json数据处理
String response_data = prev.getResponseDataAsString();
JSONObject parseObject = JSON.parseObject(response_data);
log.info("====********************************************************************"+ parseObject);
String data = parseObject.getString("data");
//这是加密的数据处理
if (data != null) {
log.info("====PostProcessor********************************************************************"+ data);
//解密
String reqDencryptJSON = AESUtis.appAESDencrypt(data, deviceId,secretKey );
log.info("===PostProcessor**********************************responseBody*****************************"+ reqDencryptJSON);
//解密的数据写回返回body中
prev.setResponseData(reqDencryptJSON.getBytes("UTF-8"));
//取用户属性保存,以便以后用到
JSONObject resultJson= JSON.parseObject(reqDencryptJSON);
JSONObject result = resultJson.getJSONObject("result");
if (result != null) {
log.info("===PostProcessor*********************************result *******===************" );
String accessKey = result.getString("accessKey");
if (accessKey != null) {
String accountId = result.getString("accountId");
String token = result.getString("token");
//配置.设置头信息和公共信息
String sign = MD5Utils.md5To32LowerCaseHexString( secretKey + accountId+ deviceId + secretKey);
//文件读取放到这里, 文件内容会被重写,而不是追加模式,所以不能实际保存内容的地方申明FileOutputStream
Properties properties = new Properties();
FileOutputStream outputStream = new FileOutputStream(fileName);
properties.setProperty("accountId",accountId);
properties.setProperty("accessKey",accessKey);
properties.setProperty("sign",sign);
properties.setProperty("token",token);
properties.store(outputStream, "");//
log.info("===PostProcessor**********************************properties.store*******===************" );
//资源释放
if (outputStream != null) {
outputStream.close();
}
}
}
}
} catch (Exception ex) {
log.info("Script execution failed================PostProcessor=========================", ex);
}