BeanShell PreProcessor添加sign,timestamp

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.commons.codec.digest.DigestUtils; 
import org.apache.jmeter.config.Arguments;
    
        

//url解码

public static String URLDecoderString(String str) {

      String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
    //获取发送的data
    Arguments args = sampler.getArguments(); 
    String req_str = args.toString().replace("(","").replace(")","");
    log.info("请求参数为:"+req_str);
    static long timestamp = System.currentTimeMillis();

        
        if (!(req_str.isEmpty())) {
            req_str =  req_str+"&timestamp="+timestamp;
        }else {
            req_str = "timestamp="+timestamp;
        }
        String postDataTimeStamp = req_str;
        String[] poStrings  = postDataTimeStamp.split("&");
        String md5BeforeString = "";

        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < poStrings.length; i++) {
            if (!poStrings[i].endsWith("=")) {
                arrayList.add(poStrings[i]);
            }             
        }
        Collections.sort(arrayList);
        System.out.println(arrayList);    

        
        for (int j = 0; j < arrayList.size(); j++) {
            if (j == arrayList.size()-1) {
                md5BeforeString = md5BeforeString + arrayList.get(j);
            }else {
                md5BeforeString = md5BeforeString + arrayList.get(j)+"&";
            }
        }    

        System.out.println(URLDecoderString(md5BeforeString));
        
        log.info(URLDecoderString(md5BeforeString));


          String md5String = DigestUtils.md5Hex(URLDecoderString(md5BeforeString));
        log.info(md5String.toUpperCase());
        vars.put("md5String",md5String.toUpperCase());
        String timestampNew = String.valueOf(timestamp); 
        vars.put("timestamp",timestampNew);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值