接受httpclient4中的任何cookie

发布于 2024-10-30 18:21:38 字数 736 浏览 3 评论 0原文

我尝试为我的网站开发Java客户端,但无法在httpclient4中存储cookie,服务器在get请求中发送标头:

Set-Cookie: PHPSESSID=ea384f86b9b89a749f1684d9d3980820; path=/

但是在我发出请求后的代码中:

CookieManager m = (CookieManager) CookieHandler.getDefault();
System.out.println("Count : " + m.getCookieStore().getCookies().size());

并且我总是获得计数:0

Httpclient创建:

CookieManager cookiem = new CookieManager();
cookiem.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookiem);
...
httpClient = new DefaultHttpClient(params);
        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, org.apache.http.client.params.CookiePolicy.BEST_MATCH);

但我获得相同的结果,怎么了?

I try to develop Java client to my site but can't store cookies in httpclient4, server send in headers in get request :

Set-Cookie: PHPSESSID=ea384f86b9b89a749f1684d9d3980820; path=/

But in code after request I make :

CookieManager m = (CookieManager) CookieHandler.getDefault();
System.out.println("Count : " + m.getCookieStore().getCookies().size());

And I always obtain Count : 0

Httpclient creation :

CookieManager cookiem = new CookieManager();
cookiem.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookiem);
...
httpClient = new DefaultHttpClient(params);
        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, org.apache.http.client.params.CookiePolicy.BEST_MATCH);

But I obtain same result, what is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜宝宝 2024-11-06 18:21:38

CookieManager 是 JRE 内部 HTTP 客户端使用的 Java 6 特定类。

Apache HttpClient 以不同的方式管理 HTTP 状态,并且不能(并且可能不应该)使用 Java 6 特定的类。

有关使用 Apache HttpClient 进行 HTTP 状态管理的详细信息,请参阅:

http:// /hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html

CookieManager is a Java 6 specific class used by JRE's internal HTTP client.

Apache HttpClient manages HTTP state differently and cannot (and probably should not) make use of Java 6 specific classes.

For details on HTTP state management with Apache HttpClient please see:

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文