[英]How can I get the cookies from HttpClient?
我正在使用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();
另一个让其他人开始,看到不存在的方法摸不着头脑......
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
的数组
请注意:第一个链接指向曾经在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。
根据初始问题中的示例,在执行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在上面对 这个答案的评论中回答的那样 ,你可以使用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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.