coffee coffee - 4 months ago 45
Java Question

How can I get the cookies from HttpClient?

I am using HttpClient 4.1.2

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


So, how can I get the cookie values?

Answer

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

The following is relevant for V4:

...in addition, the javadocs should contain more information on cookie handling

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

and here is a tutorial for httpclient v4:

http://hc.apache.org/httpcomponents-client-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...

Please make sure you read the javadocs for ResponseProcessCookies and AbstractHttpClient.