2012-01-04 188 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。