【问题标题】:How can I get the cookies from HttpClient?如何从 HttpClient 获取 cookie?
【发布时间】:2012-02-02 18:16:14
【问题描述】:

我正在使用 HttpClient 4.1.2

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

那么,如何获取 cookie 值?

【问题讨论】:

    标签: java cookies apache-httpclient-4.x


    【解决方案1】:

    不确定为什么接受的答案描述了一个不存在的方法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();
    

    【讨论】:

    • 接受的答案适用于以前版本的 v4。这种方法适用于最新的方法。
    • 是的,这应该是公认的答案。我只是将旧的 httpclient(3.x 到 4.3)迁移到新的,这个答案真的很有帮助。
    • 你可以做得更简单:CloseableHttpClient httpclient = httpClients.custom().setDefaultCookieStore(cookies).build();
    【解决方案2】:

    另一个让其他人开始,看到不存在的方法挠头......

    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头域,所以需要获取Headers的数组

    【讨论】:

      【解决方案3】:

      请注意:第一个链接指向曾经在 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 的 javadocs。

      【讨论】:

      • 好的,明白了:请查看 ResponseProcessCookies 类了解更多详细信息。 HttpClientv4 似乎使用它(默认情况下)来填充 cookie 存储。您可以从 AbstractHttpClient.getCookieStore 访问它
      • 这就是问题所在。找不到方法httpClient.getCookieStore();
      • 对不起,如果我的疑问太简单了,但我该如何调用这个方法?
      • org.apache.http.impl.client.AbstractHttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient() 该演员似乎允许您获取 cookie 存储
      • 此已弃用已过时,请参阅下面的@Alex 答案或查看文档:hc.apache.org/httpcomponents-client-ga/tutorial/html/…
      【解决方案4】:

      根据第一个问题中的示例,在执行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 时。

      【讨论】:

        【解决方案5】:

        正如Matt Broekhuisthis answer above的评论中回答的那样,您可以使用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]]
        

        【讨论】:

        • 可能是因为org.apache.http.impl.client.DefaultHttpClient 已被弃用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多