beanshell处理响应头cookie问题系列一

import java.io.*;
import java.util.*;
import com.alibaba.fastjson2.*;

log.info("header开始");
log.info(prev.getResponseHeaders());
log.info("header结束");
String reponseHeaders = prev.getResponseHeaders();

String[] hlist = reponseHeaders.split("\n"); 
log.info(Arrays.toString(hlist));

hlistlen = hlist.length;         
log.info("长度=====");
log.info(hlistlen.toString());

Map headersMap = new HashMap();   
Map headersMap1 = new HashMap();
Map headersMap2 = new HashMap();

log.info("第"+0+"个:"+hlist[0]);
log.info("第"+7+"个:"+hlist[7]); 
log.info("第"+10+"个:"+hlist[10]); 

String[] itemlist1 = hlist[7].split(":");
headersMap1.put(itemlist1[0],itemlist1[1]); 
log.info("1111: "+itemlist1[1]);
//headersMap1.put(itemlist1[0],"123"); 
// cookie1 : SESSION=123
cookie1 = headersMap1.get("Set-Cookie");
log.info("cookie1值为===="+ cookie1);
String[] cookiee1 = cookie1.split(";");
//String cookiee1 = cookie1.split(";");
log.info("77777777777777=====下面这个是正确的cookie: ");
log.info(cookiee1[0].toString());
vars.put("ac",cookiee1[0].toString());//这个地方就是类似个map,这里存value进key,下方使用${key}即可获取,为啥下方取的cookie,session不对,下方http调用前需要通过cookieManager清除一下自带的cookie.
log.info("ac=: "+vars.get("ac"));
//log.info(cookiee1);
String cookiee11 = cookiee1[0].toString();
//String c = cookiee11.substring(9,57);
String c = "abgv";
vars.put("token",c);
log.info("77777777777777=====上面这个是正确的cookie: "+c);
log.info("7777777token7777777=====上面这个是正确的token1: "+token);
log.info("7777777token7777777=====上面这个是正确的token2: "+vars);
log.info("7777777token7777777=====上面这个是正确的token3: "+vars.get("token"));
vars.put("session",c.toString());
log.info("777777788888888887777777=====: "+session);
String var1 = vars.get("session");
//vars.put("d_MainView_results_AttrTab_GCMC_initLoadPage_Submit_C_2",GCMC);
//vars.put("session","cookiee1");
log.info("77777777777777=====: "+var1);

String[] itemlist2 = hlist[10].split(":");
headersMap2.put(itemlist2[0],itemlist2[1]); 
//headersMap2.put(itemlist2[0],"456"); 
log.info("2222 :"+itemlist2[1]);
cookie2 = headersMap2.get("Set-Cookie");
log.info("cookie2值为===="+ cookie2);
String[] cookiee2 = cookie2.split(";");
log.info("1010010101010=====下面这个是正确的cookie: ");
log.info(cookiee2[0]);
log.info("1010010101010=====上面这个是正确的cookie: ");

for(i=1;i     log.info("第"+i+"个:"+hlist[i]);    
    String[] itemlist = hlist[i].split(":");    
    headersMap.put(itemlist[0],itemlist[1]);  
}

log.info(headersMap.toString());          

cookie = headersMap.get("Set-Cookie"); 
log.info("cookie值为===="+ cookie);
String[] cookiee = cookie.split(";");   
log.info(cookiee[0]);

这一步操作是点睛之笔,是我想不明白的误区.

vars.put("ac",cookiee1[0].toString());//这个地方就是类似个map,这里存value进key,下方使用${key}即可获取,为啥下方取的cookie,session不对,下方http调用前需要通过cookieManager清除一下自带的cookie.

如果发现cookie还是原来自带的,那么只需要再下一个请求之前清理,清除一下cookie即可.

import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;

CookieManager cManager = sampler.getCookieManager();
int count=cManager.getCookieCount();
for (int index = 0;index     cManager.remove(0);
}

整体思路:

取固定正确cookie+运行其他接口之前清除自带cookie, 这是核心要解决的问题。

你可能感兴趣的:(java,算法,python)