How can I get the cookies from HttpClient?

前端 未结 5 1750
难免孤独
难免孤独 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<Cookie> cookies = cookieStore.getCookies();
    // process...
    

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

    0 讨论(0)
  • 2020-12-14 06:12

    Not sure why the accepted answer describes a method getCookieStore() that does not exist. That is incorrect.

    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.

    /* 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();
    
    0 讨论(0)
  • 2020-12-14 06:14

    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 Headers

    0 讨论(0)
  • 2020-12-14 06:26

    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.

    HttpContext will reference a cookie store (new if no CookieStore was specified in the HttpClientBuilder) after a request is executed.

    HttpClientContext context = new HttpClientContext();
    CloseableHttpResponse response = httpClient.execute(request, context);
    CookieStore cookieStore = context.getCookieStore();
    

    This applies on httpcomponents-client:4.3+ when the ClosableHttpClient was introduced.

    0 讨论(0)
  • 2020-12-14 06:34

    As Matt Broekhuis answered in a comment on this answer above, you can use 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.

    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);
      }
    }
    

    which outputs

    [[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 讨论(0)
提交回复
热议问题