Jmeter-动态获取cookie

1 使用正则提取器

1.1 样例

返回值:

{"status":1,"code":null,"message":null,"object":{"id":4183,"phone":null,"gender":null,"username":"ssxiaowei1","avatar":null,"realName":null,"authStatus":1,"accessToken":"lXlbxztnrditwPyM85TenVupF5FMNWnU/8E/dYqMq9Xvi1xgFm3GkVPDd98jFGsg","refreshToken":"oUWIIuaj8gNPKA9111M28FziJhXxAvpo4AZSBzgMjcjPKVNVDWu6eAfSsrbuTq7LAqIkVS1ziNCT93fqlSFn1DViSZhbxFQtTO2ll7Wm/UN4J9Vq63fY0LojaC+ZqMKwUSlLR/SO8BjABLbCu6hx6g=="}}

1.2 正则提取器:

1.3 HTTP头信息管理

将正则提取器提取的数据引入HTTP头信息管理中,例:TOKEN=${set_cookie_g1}; REFRESHTOKEN=${set_cookie_g2}; PLATFORMID=5; SYSTEMTD=2;

注,除变量名之外的是当前所需cookie的固定内容,直接拼接出cookie的格式即可。

2 Beanshell后置处理器

import org.apache.jmeter.samplers.*;
import org.apache.jmeter.config.*; 
import java.util.regex.Matcher;
import java.util.regex.Pattern;

String headers=prev.getResponseHeaders();//获取前一个请求的响应头部信息
log.info("headers-->{}",headers);
String regex = "Set-Cookie:(.*)"; //正则匹配到Cookie信息
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(headers);
while(m.find()){
  String cookie=m.group(1);
   log.info("cookie-->{}",cookie);
   String cookietype=cookie.split("=")[0];
   log.info("cookietype--{}",cookietype);
   switch(cookietype){
   case " t_skey":
     String str=cookie.split(";")[0];
     log.info("t_skey--{}",str.split("=")[1]);
     vars.put("t_skey",cookie.split(";")[0].split("=")[1]);
   case " LMB_AUTH_CODE":
     log.info("LMB_AUTH_CODE--{}",cookie.split(";")[0].split("=")[1]);
     vars.put("LMB_AUTH_CODE",cookie.split(";")[0].split("=")[1]);
   case " __TOKEN_NEW":
     log.info("__TOKEN_NEW--{}",cookie.split(";")[0].split("=")[1]);
     vars.put("__TOKEN_NEW",cookie.split(";")[0].split("=")[1]);     
   }
 }
 log.info("__TOKEN_NEW-->{}",vars.get("__TOKEN_NEW"));
 log.info("LMB_AUTH_CODE-->{}",vars.get("LMB_AUTH_CODE"));
 log.info("t_skey-->{}",vars.get("t_skey"));

 

posted @ 2021-09-13 22:43  术科术  阅读(270)  评论(0编辑  收藏  举报