45

我正在使用 HttpClient 4.1.2

HttpGet httpget = new HttpGet(uri); 
HttpResponse response = httpClient.execute(httpget);

那么,如何获取 cookie 值呢?

4

5 回答 5

60

不知道为什么接受的答案描述了一种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();
于 2015-03-10T20:36:49.050 回答
14

另一个让其他人开始,看到不存在的方法挠头......

import org.apache.http.Header;
import org.apache.http.HttpResponse;

Header[] headers = httpResponse.getHeaders("Set-Cookie");
for (Header h : headers) {
    System.out.println(h.getValue().toString());  
}

这将打印 cookie 的值。服务器响应可以有多Set-Cookie个头域,所以你需要检索一个Headers的数组

于 2013-12-27T14:27:35.933 回答
8

请注意:第一个链接指向曾经在 HttpClient V3 中工作的东西。在下方查找 V4 相关信息。

这应该回答你的问题

http://www.java2s.com/Code/Java/Apache-Common/GetCookievalueandsetcookievalue.htm

以下内容与 V4 相关:

...此外,javadocs 应包含有关 cookie 处理的更多信息

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html

这是httpclient v4的教程:

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html

这是一些有帮助的伪代码(我希望它仅基于文档):

HttpClient httpClient = new DefaultHttpClient();
// execute get/post/put or whatever
httpClient.doGetPostPutOrWhatever();
// get cookieStore
CookieStore cookieStore = httpClient.getCookieStore();
// get Cookies
List<Cookie> cookies = cookieStore.getCookies();
// process...

请确保您阅读了 ResponseProcessCookies 和 AbstractHttpClient 的 javadocs。

于 2012-01-04T20:49:00.343 回答
6

根据第一个问题中的示例,CookieStore在执行 HTTP 请求后访问的方式是使用HttpContext执行状态对象。

HttpContext执行请求后将引用 cookie 存储(如果在 HttpClientBuilder 中未指定 CookieStore,则为新存储)。

HttpClientContext context = new HttpClientContext();
CloseableHttpResponse response = httpClient.execute(request, context);
CookieStore cookieStore = context.getCookieStore();

这适用于httpcomponents-client:4.3+何时ClosableHttpClient引入。

于 2017-10-06T16:47:38.617 回答
1

正如Matt Broekhuis在上面对此答案的评论中回答的那样,您可以使用DefaultHttpClient.getCookieStore()

请注意,在我回答时,我的服务器仅限于httpclient-4.2.5. DefaultHttpClient现在从 4.3 起已弃用。我将把这个答案留在这里,因为其他人可能会发现自己处于同样的情况,并且原始海报指定他们使用的是 4.1.2。

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.util.List;

public class So8733758 {

  public static void main(String... args) throws IOException {
    final HttpUriRequest request = new HttpGet("http://stackoverflow.com");
    final DefaultHttpClient http = new DefaultHttpClient();
    http.execute(request);
    final List<Cookie> cookies = http.getCookieStore().getCookies();
    System.out.println(cookies);
  }
}

哪个输出

[[version: 0][name: __cfduid][value: de2dfa8314f565701cf7b3895206f04d81457380383][domain: .stackoverflow.com][path: /][expiry: Tue Mar 07 11:53:03 PST 2017], [version: 0][name: prov][value: eeee9738-c50b-44f6-a8aa-b54966db1a88][domain: .stackoverflow.com][path: /][expiry: Thu Dec 31 16:00:00 PST 2054]]
于 2016-03-07T20:02:00.890 回答