完整beanshell代碼java
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);