如何从HttpClient类型的现有对象获取cookie? 我正在使用HttpClient版本4.3.3,它没有方法httpClient.getCookieStore()了。
答案 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();