如何从HttpClient获取cookie?


问题内容

我正在使用HttpClient 4.1.2

HttpGet httpget = new HttpGet(uri); 
HttpResponse response = httpClient.execute(httpget);

那么,如何获取Cookie值?


问题答案:

请注意:第一个链接指向曾经在HttpClient V3中工作的内容。在下面找到与V4相关的信息。

这应该回答你的问题

http://www.java2s.com/Code/Java/Apache-
Common/GetCookievalueandsetcookievalue.htm

以下与V4有关:

…此外,javadocs还应包含有关Cookie处理的更多信息

http://hc.apache.org/httpcomponents-client-
ga/httpclient/apidocs/index.html

这是httpclient v4的教程:

http://hc.apache.org/httpcomponents-client-
ga/tutorial/html/index.html

这是一些有用的伪代码(我希望,它仅基于文档):

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的javadocs。