【发布时间】:2012-02-02 18:16:14
【问题描述】:
我正在使用 HttpClient 4.1.2
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpClient.execute(httpget);
那么,如何获取 cookie 值?
【问题讨论】:
标签: java cookies apache-httpclient-4.x
我正在使用 HttpClient 4.1.2
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpClient.execute(httpget);
那么,如何获取 cookie 值?
【问题讨论】:
标签: java cookies apache-httpclient-4.x
不确定为什么接受的答案描述了一个不存在的方法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();
【讨论】:
CloseableHttpClient httpclient = httpClients.custom().setDefaultCookieStore(cookies).build();
另一个让其他人开始,看到不存在的方法挠头......
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的数组
【讨论】:
请注意:第一个链接指向曾经在 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。
【讨论】:
httpClient.getCookieStore();
根据第一个问题中的示例,在执行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 时。
【讨论】:
正如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]]
【讨论】:
org.apache.http.impl.client.DefaultHttpClient 已被弃用。