I am using HttpClient 4.1.2
我使用HttpClient 4.1.2。
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpClient.execute(httpget);
So, how can I get the cookie values?
那么,如何获得cookie值呢?
5 个解决方案
#1
10
Please Note: The first link points to something that used to work in HttpClient V3. Find V4-related info below.
请注意:第一个链接指向HttpClient V3中使用的东西。找到V4-related下面的信息。
This should answer your question
这应该能回答你的问题。
http://www.java2s.com/Code/Java/Apache-Common/GetCookievalueandsetcookievalue.htm
http://www.java2s.com/Code/Java/Apache-Common/GetCookievalueandsetcookievalue.htm
The following is relevant for V4:
以下与V4有关:
...in addition, the javadocs should contain more information on cookie handling
…此外,javadocs应该包含更多关于cookie处理的信息。
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html
and here is a tutorial for httpclient v4:
下面是httpclient v4的教程:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
And here is some pseudo-code that helps (I hope, it's based only on docs):
这里有一些伪代码可以帮助(我希望它只基于文档):
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...
Please make sure you read the javadocs for ResponseProcessCookies and AbstractHttpClient.
请确保您阅读了ResponseProcessCookies和AbstractHttpClient的javadocs。
#2
37
Not sure why the accepted answer describes a method getCookieStore()
that does not exist. That is incorrect.
不知道为什么接受的答案描述了一个不存在的方法getCookieStore()。这是不正确的。
You must create a cookie store beforehand, then build the client using that cookie store. Then you can later refer to this cookie store to get a list of cookies.
您必须事先创建一个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://*.com/");
HttpResponse httpResponse = null;
try {httpResponse = http.execute(httpRequest);} catch (Throwable error) {throw new RuntimeException(error);}
/* check cookies */
httpCookieStore.getCookies();
#3
9
Yet another to get other people started, seeing non-existent methods scratching their heads...
还有一件事让其他人开始,看到不存在的方法让他们挠头……
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());
}
This will print the values of the cookies. The server response can have several Set-Cookie
header fields, so you need to retrieve an array of Header
s
这将打印cookie的值。服务器响应可以有多个Set-Cookie头字段,因此需要检索一个头数组
#4
1
As Matt Broekhuis answered in a comment on this answer above, you can use DefaultHttpClient.getCookieStore()
正如Matt Broekhuis在上面回答的那样,您可以使用DefaultHttpClient.getCookieStore()
Note, that at the time that I answered my server was limited to httpclient-4.2.5
. DefaultHttpClient
is now deprecated as of 4.3. I'm going to leave this answer here because others might find themselves in the same situation and the original poster specified they were using 4.1.2.
注意,在我回答我的服务器时,它仅限于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://*.com");
final DefaultHttpClient http = new DefaultHttpClient();
http.execute(request);
final List<Cookie> cookies = http.getCookieStore().getCookies();
System.out.println(cookies);
}
}
which outputs
的输出
[[version: 0][name: __cfduid][value: de2dfa8314f565701cf7b3895206f04d81457380383][domain: .*.com][path: /][expiry: Tue Mar 07 11:53:03 PST 2017], [version: 0][name: prov][value: eeee9738-c50b-44f6-a8aa-b54966db1a88][domain: .*.com][path: /][expiry: Thu Dec 31 16:00:00 PST 2054]]
#5
1
Based on the example in the initial question, the way to access the CookieStore
after executing an HTTP request, is by using the HttpContext
execution state object, which will reference a cookie store (new if no CookieStore was specified in the HttpClientBuilder, or a new CookieStore) after a request is executed.
基于最初的问题中的示例,方法访问CookieStore执行HTTP请求后,通过使用HttpContext对象执行状态,将引用一个曲奇饼店(新如果没有CookieStore HttpClientBuilder中指定,或一个新的CookieStore)后执行请求。
HttpClientContext context = new HttpClientContext();
CloseableHttpResponse response = httpClient.execute(request, context);
CookieStore cookieStore = context.getCookieStore();
This is based on httpcomponents-client:4.3+ when the ClosableHttpClient and InternalHttpClient were introduced.
这是基于ClosableHttpClient和InternalHttpClient引入时的httpcomponent -client:4.3+。
#1
10
Please Note: The first link points to something that used to work in HttpClient V3. Find V4-related info below.
请注意:第一个链接指向HttpClient V3中使用的东西。找到V4-related下面的信息。
This should answer your question
这应该能回答你的问题。
http://www.java2s.com/Code/Java/Apache-Common/GetCookievalueandsetcookievalue.htm
http://www.java2s.com/Code/Java/Apache-Common/GetCookievalueandsetcookievalue.htm
The following is relevant for V4:
以下与V4有关:
...in addition, the javadocs should contain more information on cookie handling
…此外,javadocs应该包含更多关于cookie处理的信息。
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html
and here is a tutorial for httpclient v4:
下面是httpclient v4的教程:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
And here is some pseudo-code that helps (I hope, it's based only on docs):
这里有一些伪代码可以帮助(我希望它只基于文档):
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...
Please make sure you read the javadocs for ResponseProcessCookies and AbstractHttpClient.
请确保您阅读了ResponseProcessCookies和AbstractHttpClient的javadocs。
#2
37
Not sure why the accepted answer describes a method getCookieStore()
that does not exist. That is incorrect.
不知道为什么接受的答案描述了一个不存在的方法getCookieStore()。这是不正确的。
You must create a cookie store beforehand, then build the client using that cookie store. Then you can later refer to this cookie store to get a list of cookies.
您必须事先创建一个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://*.com/");
HttpResponse httpResponse = null;
try {httpResponse = http.execute(httpRequest);} catch (Throwable error) {throw new RuntimeException(error);}
/* check cookies */
httpCookieStore.getCookies();
#3
9
Yet another to get other people started, seeing non-existent methods scratching their heads...
还有一件事让其他人开始,看到不存在的方法让他们挠头……
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());
}
This will print the values of the cookies. The server response can have several Set-Cookie
header fields, so you need to retrieve an array of Header
s
这将打印cookie的值。服务器响应可以有多个Set-Cookie头字段,因此需要检索一个头数组
#4
1
As Matt Broekhuis answered in a comment on this answer above, you can use DefaultHttpClient.getCookieStore()
正如Matt Broekhuis在上面回答的那样,您可以使用DefaultHttpClient.getCookieStore()
Note, that at the time that I answered my server was limited to httpclient-4.2.5
. DefaultHttpClient
is now deprecated as of 4.3. I'm going to leave this answer here because others might find themselves in the same situation and the original poster specified they were using 4.1.2.
注意,在我回答我的服务器时,它仅限于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://*.com");
final DefaultHttpClient http = new DefaultHttpClient();
http.execute(request);
final List<Cookie> cookies = http.getCookieStore().getCookies();
System.out.println(cookies);
}
}
which outputs
的输出
[[version: 0][name: __cfduid][value: de2dfa8314f565701cf7b3895206f04d81457380383][domain: .*.com][path: /][expiry: Tue Mar 07 11:53:03 PST 2017], [version: 0][name: prov][value: eeee9738-c50b-44f6-a8aa-b54966db1a88][domain: .*.com][path: /][expiry: Thu Dec 31 16:00:00 PST 2054]]
#5
1
Based on the example in the initial question, the way to access the CookieStore
after executing an HTTP request, is by using the HttpContext
execution state object, which will reference a cookie store (new if no CookieStore was specified in the HttpClientBuilder, or a new CookieStore) after a request is executed.
基于最初的问题中的示例,方法访问CookieStore执行HTTP请求后,通过使用HttpContext对象执行状态,将引用一个曲奇饼店(新如果没有CookieStore HttpClientBuilder中指定,或一个新的CookieStore)后执行请求。
HttpClientContext context = new HttpClientContext();
CloseableHttpResponse response = httpClient.execute(request, context);
CookieStore cookieStore = context.getCookieStore();
This is based on httpcomponents-client:4.3+ when the ClosableHttpClient and InternalHttpClient were introduced.
这是基于ClosableHttpClient和InternalHttpClient引入时的httpcomponent -client:4.3+。