接口测试过程中需要保持用户登陆状态,这里说一下设置cookies的方法
获取cookies首先要登陆成功,
1.创建httpclient时添加cookies
//new一个cookiestore对象
CookieStore cookieStore = new BasicCookieStore(); //添加cookie BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "f08f076d-5490-4a72-b1eb-48b58f7ab84c"); //放入cookiestore cookieStore.addCookie(cookie); //声明httpclient,同时添加上cookies CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCookieStore(cookieStore)//设置Cookie .build();
2.在headers中添加cookies
httpPost.addHeader("JSESSIONID","7f012e0e-488b-43ff-a031-9161b9957876");