我正在使用 HttpClient 4.1.2
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpClient.execute(httpget);
那么,如何获取 cookie 值?
最佳答案
不确定为什么接受的答案描述了一个不存在的方法 getCookieStore()
。这是不正确的。
您必须事先创建一个 cookie 存储,然后使用该 cookie 存储构建客户端。然后您可以稍后引用此 cookie 存储以获取 cookie 列表。
/* init client */
HttpClient http = null;
CookieStore httpCookieStore = new BasicCookieStore();
HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore);
http = builder.build();
/* do stuff */
HttpGet httpRequest = new HttpGet("http://stackoverflow.com/");
HttpResponse httpResponse = null;
try {httpResponse = http.execute(httpRequest);} catch (Throwable error) {throw new RuntimeException(error);}
/* check cookies */
httpCookieStore.getCookies();
关于java - 如何从 HttpClient 获取 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8733758/