我试图在发送 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());
}
}
谢谢