jmeter -- 在beanshell中处理http请求数据,对参数进行签名

完整beanshell代码

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.Header;
import java.util.*;
import com.google.gson.Gson;
import org.apache.commons.codec.digest.DigestUtils;
import java.security.NoSuchAlgorithmException;
//生成签名的方法
String signURLAndRequestParams() throws NoSuchAlgorithmException {

省略。。。

return sign;
}

params = new ArrayList();
//获取请求的参数
Arguments args = sampler.getArguments();
//获取header中token
String token = null;
HeaderManager headerManager = sampler.getHeaderManager();
headerMap = new HashMap();
try{
if(headerManager != null){
for(int i=0;i<headerManager.size();i++){
Header header = headerManager.get(i);
headerMap.put(header.getName(),header.getValue());
}
}

}
catch(Exception e){
log.info(e.getMessage());
}
if(headerMap.containsKey("Authorization")){
token = headerMap.get("Authorization");
}
//参数转换,添加到params
it = args.iterator();
while(it.hasNext()){
argument = it.next();
String pair = argument.toString();
if(pair.startsWith("=")){
params.add("jsonBody"+pair);
}
else{
params.add(pair);
}
}

//获取path
String path = sampler.getPath();
//从变量获取timestamp、app_id,secret_key
String timestamp = vars.get("timestamp");
String app_id = vars.get("app_id");
String appSecret = vars.get("secretKey");
//把app_id,timestamp添加到params中
String timestamp_param = "timestamp="+timestamp;
String app_id_param = "app_id="+app_id;
params.add(timestamp_param);
params.add(app_id_param);
//获取method
String method = sampler.getMethod();
//获取签名
String sign = signURLAndRequestParams(path,params,appSecret,method,token);
//把app_id、timestamp、sign加到path
String path_send = path+"?"+app_id_param+"&"+timestamp_param+"&sign="+sign;
//reset path
sampler.setPath(path_send);

 

posted @ 2018-11-13 17:23  弘文馆校书  阅读(4527)  评论(3编辑  收藏  举报