jmeter请求数据加密在发送方法
import java.security.MessageDigest;
import org.json.*;
//import java.util.Date;
//import java.text.SimpleDateFormat;
//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//String currTime = df.format(new Date());
String token = vars.get("token");
//此处参数化开始
//json 请求data 对象值填充
JSONObject data_obj = new JSONObject();
data_obj.put("ukey",token);
data_obj.put("page",vars.get("page"));
data_obj.put("rows",vars.get("page"));
String method = "tms_driver_deliveryhis"; //设置接口请求的 方法名 tms_driver_deliveryhis
//此处参数化结束
//格式化参数
String timestamp = vars.get("timestamp");
String data = data_obj.toString();
data = data.replaceAll("\"", "\'");
//log.info("data " + data);
String appkey = vars.get("appkey");
String dataformat = vars.get("dataformat");
String v= vars.get("v");
String sign_method = vars.get("sign_method");
String secret_key = vars.get("secret_key");
String reSign = appkey+method+v+data+secret_key+timestamp;
String reSign = reSign;
public String toMD5(String plainText) {
try {
//生成实现指定摘要算法的 MessageDigest 对象。
MessageDigest md = MessageDigest.getInstance("MD5");
//使用指定的字节数组更新摘要。
md.update(plainText.getBytes());
//通过执行诸如填充之类的最终操作完成哈希计算。
byte[] b = md.digest();
//生成具体的md5密码到buf数组
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
System.out.println("32位: " + buf.toString());// 32位的加密
System.out.println("16位: " + buf.toString().substring(8, 24));// 16位的加密,其实就是32位加密后的截取
return buf.toString();
}
catch (Exception e) {
e.printStackTrace();
}
}
String sign = toMD5(reSign);
String sign = sign.toUpperCase();
//log.info("MD5Sign "+sign);
//拼接请求字符串
String requestStr = "data="+data+"&appkey="+appkey+"&dataformat="+dataformat+"&v="+v+"&sign_method="+sign_method+"&method="+method+"×tamp="+timestamp+"&sign="+sign;
log.info("requestStr "+requestStr);
vars.put("requestStr",requestStr);