如何从HttpClient获取cookie?

时间:2012-01-04 20:46:40

标签: java cookies apache-httpclient-4.x

我正在使用HttpClient 4.1.2

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

那么,我怎样才能获得cookie值?

5 个答案:

答案 0 :(得分:45)

不确定为什么接受的答案会描述不存在的方法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();

答案 1 :(得分:12)

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

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标头字段,因此您需要检索Header s

的数组

答案 2 :(得分:9)

请注意:第一个链接指向过去在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的javadoc。

答案 3 :(得分:4)

根据初始问题中的示例,执行HTTP请求后访问CookieStore的方法是使用HttpContext执行状态对象。

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

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

httpcomponents-client:4.3+被引入时,这适用于ClosableHttpClient

答案 4 :(得分:1)

Matt Broekhuis的评论中回答this answer above,您可以使用DefaultHttpClient.getCookieStore()

请注意,我回答服务器时仅限httpclient-4.2.5。自{4.3}起,DefaultHttpClient已被弃用。我将在这里留下这个答案,因为其他人可能会发现自己处于同样的情况,并且原始海报指出他们使用的是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]]