How do I get the cookies from Apache HttpClient 4.x?

eztam picture eztam · Mar 5, 2014 · Viewed 14.4k times · Source

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.

Answer

ok2c picture ok2c · Mar 6, 2014
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();
}