如何从 HttpClient 获取 cookie? [英] How can I get the cookies from HttpClient?
问题描述
我使用的是 HttpClient 4.1.2
I am using HttpClient 4.1.2
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpClient.execute(httpget);
那么,我如何获取 cookie 值?
So, how can I get the cookie values?
推荐答案
请注意:第一个链接指向曾经在 HttpClient V3 中工作的内容.在下方查找与 V4 相关的信息.
Please Note: The first link points to something that used to work in HttpClient V3. Find V4-related info below.
这应该可以回答您的问题
This should answer your question
http://www.java2s.com/Code/Java/Apache-Common/GetCookievalueandsetcookievalue.htm
以下内容与 V4 相关:
The following is relevant for V4:
...此外,javadocs 应该包含更多关于 cookie 处理的信息
...in addition, the javadocs should contain more information on cookie handling
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html
这里是 httpclient v4 的教程:
and here is a tutorial for httpclient v4:
http://hc.apache.org/httpcomponents-客户端-ga/tutorial/html/index.html
这是一些有用的伪代码(我希望它仅基于文档):
And here is some pseudo-code that helps (I hope, it's based only on docs):
HttpClient httpClient = new DefaultHttpClient();
// execute get/post/put or whatever
httpClient.doGetPostPutOrWhatever();
// get cookieStore
CookieStore cookieStore = httpClient.getCookieStore();
// get Cookies
List<Cookie> cookies = cookieStore.getCookies();
// process...
请确保您阅读了 ResponseProcessCookies 和 AbstractHttpClient 的 javadoc.
Please make sure you read the javadocs for ResponseProcessCookies and AbstractHttpClient.
这篇关于如何从 HttpClient 获取 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!