如何从Apache HttpClient 4.x获取cookie?

时间:2014-03-05 17:51:10

标签: httpclient apache-httpclient-4.x apache-commons-httpclient

如何从HttpClient类型的现有对象获取cookie? 我正在使用HttpClient版本4.3.3,它没有方法httpClient.getCookieStore()了。

2 个答案:

答案 0 :(得分:17)

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

答案 1 :(得分:0)

版本4.5.2 您可以使用以下代码:

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