coffee coffee - 1 year ago 147
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 Source

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

The following is relevant for V4: addition, the javadocs should contain more information on cookie handling

and here is a tutorial for httpclient v4:

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
// 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.