接口測試過程中需要保持用戶登陸狀態,這里說一下設置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");