我试图在发送http请求之前设置abc=123的cookie。
然后在响应中,我期望收到相同的cookie。但是实际响应中我收到的是abc=890,这个值是由目标服务器设置的。
DefaultHttpClient httpclient = new DefaultHttpClient();
CookieStore cookieStore = httpclient.getCookieStore();
BasicClientCookie cookie = new BasicClientCookie("abc", "123");
// Prepare a request object
HttpGet httpget = new HttpGet("http://abc.net/restofurl");
cookieStore.addCookie(cookie);
httpclient.setCookieStore(cookieStore);
// Execute the request
HttpResponse response = httpclient.execute(httpget);
// Examine the response status
log.info("Http request response is: " + response.getStatusLine());
List<Cookie> cookies = cookieStore.getCookies();
for (int i=0; i<cookies.size();i++) {
if (cookies.get(i).getName().toString().equals("abc")) {
log.info("cookie is: " + cookies.get(0).getValue().toString());
}
}
谢谢