How do I get the cookies from an existing object of type HttpClient? I'm using HttpClient version 4.3.3 which has no method httpClient.getCookieStore() anymore.
Asked
Active
Viewed 1.6k times
2 Answers
24
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
CloseableHttpResponse response = httpclient.execute(new HttpGet("/"), context);
try {
CookieStore cookieStore = context.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
} finally {
response.close();
}

ok2c
- 26,450
- 5
- 63
- 71
-
1I hoped it is possible to get the the CookieStore of the HttpClient without creating an own HttpClientContext like in version 3. – eztam Mar 06 '14 at 11:35
-
2@eztam: Why is that? HttpClient always needs an execution context, so if it is not given explicitly one will be created behind the scene. – ok2c Mar 06 '14 at 14:00
-
@ok2c because it causes a @#$@#$@#$ pain in the backside in certain perfectly normal scenarios – Yannick Menager May 30 '20 at 18:42
-
@YannickMenager What exactly cases the said pain in one's backside? Execution context? – ok2c May 30 '20 at 20:30
0
version 4.5.2 You can use this code:
List<Cookie> cookies = ((CookieStore)localContext.getAttribute(HttpClientContext.COOKIE_STORE)).getCookies();

dSH
- 143
- 6