2012-01-04 92 views

回答

10

请注意:第一个链接指向过去在HttpClient V3中工作的东西。在下面找到与V4相关的信息。

这应该回答你的问题

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

以下是相关V4:

...此外,JavaDoc就会包含在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。

+0

刚添加链接,javadoc,了解当前版本 – mkro 2012-01-04 20:52:43

+0

好的,知道了:请看看在类ResponseProcessCookies了解更多详情。 HttpClientv4似乎使用它(默认情况下)来填充cookie存储。您可以从AbstractHttpClient.getCookieStore访问它 – mkro 2012-01-04 22:01:35

+25

这是问题所在。我找不到方法'httpClient.getCookieStore();' – coffee 2012-01-05 12:21:49

9

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

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

37

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

上述工程的4.5版本。 – Paul 2015-07-19 14:03:22

+3

接受的答案适用于以前版本的v4。这种方法适用于最新的方法。 – Peter 2015-11-19 09:00:29

+1

是的,这应该是被接受的答案。我只是将旧的httpclient(3.x到4.3)迁移到新的,这个答案真的有帮助。 – 2016-02-04 13:46:28

1

由于Matt Broekhuis回答的评论this answer above,您可以使用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]] 
+0

我总是好奇,为什么要投票? – Kirby 2016-09-09 22:58:45

+1

大概是因为'org.apache.http.impl.client.DefaultHttpClient'已被弃用。 – borgmater 2017-01-10 09:43:55

1

基于在最初的问题的例子中,方式执行的HTTP请求之后访问CookieStore,是通过使用HttpContext执行状态对象,这将引用一个在执行请求之后,cookie存储(如果在HttpClientBuilder中没有指定CookieStore,或者新的CookieStore,则新建)。

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

这是基于httpcomponents的客户端上:4.3及更高版本时引入的ClosableHttpClient和InternalHttpClient。