eztam
eztam

Reputation: 3831

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

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.

Upvotes: 10

Views: 16338

Answers (2)

dSH
dSH

Reputation: 143

version 4.5.2 You can use this code:

List<Cookie> cookies = ((CookieStore)localContext.getAttribute(HttpClientContext.COOKIE_STORE)).getCookies();

Upvotes: 0

ok2c
ok2c

Reputation: 27538

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();
}

Upvotes: 24

Related Questions