How can I get the cookies from HttpClient?

前端 未结 5 1751
难免孤独
难免孤独 2020-12-14 06:08

I am using HttpClient 4.1.2

HttpGet httpget = new HttpGet(uri); 
HttpResponse response = httpClient.execute(httpget);

So, how can I get the

5条回答
  •  时光说笑
    2020-12-14 06:12

    Please Note: The first link points to something that used to work in HttpClient V3. Find V4-related info below.

    This should answer your question

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

    The following is relevant for V4:

    ...in addition, the javadocs should contain more information on cookie handling

    http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html

    and here is a tutorial for httpclient v4:

    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 cookies = cookieStore.getCookies();
    // process...
    

    Please make sure you read the javadocs for ResponseProcessCookies and AbstractHttpClient.

提交回复
热议问题