Unirest将上一个请求的cookie带到下一个请求中

开发中,在后台模拟接口请求,常常需要将第一个登录接口的Cookie保留下来,掉用第二个接口的时候,需要把这个Cookie带过去,我们这次以Unirest为例,Unirest它默认是不会将上一个请求的Cookie带到下一个请求的,我们需要做如下设置。

CookieStore cookieStore = new org.apache.http.impl.client.BasicCookieStore();

Unirest.setHttpClient(org.apache.http.impl.client.HttpClients.custom().setDefaultCookieStore(cookieStore).build());

 

 

写个例子:

CookieStore cookieStore = new org.apache.http.impl.client.BasicCookieStore();
Unirest.setHttpClient(org.apache.http.impl.client.HttpClients.custom().setDefaultCookieStore(cookieStore).build());
Unirest.post("http://www.xxx.com/login.do")
					  .field("username", username)
					  .field("userpass", "123456")
					  .field("action", "login")
					  .asString();
Unirest.post("http://www.xxx.com/index.do")
					  .field("name", username)
					  .asString();
正在加载评论...