Cookie 在 Apache httpclient 4.4 中被忽略 [英] Cookies getting ignored in Apache httpclient 4.4
问题描述
我将 Apache http 客户端从 4.3.6 升级到 4.4,发现 cookie 被忽略了.知道如何让 cookie 在 4.4 中工作吗?
I upgraded Apache http client from 4.3.6 to 4.4 and observed that cookies are getting ignored. Any idea how to get cookie working in 4.4?
代码片段
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(new BasicClientCookie("name", "value"));
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();
HttpClient client = HttpClientBuilder.create()
.disableRedirectHandling()
.setDefaultRequestConfig(config)
.setDefaultCookieStore(cookieStore)
.build();
我尝试了 CookieSpecs.DEFAULT
、CookieSpecs.STANDARD
和 CookieSpecs.STANDARD_STRICT
但似乎都不起作用.
I tried CookieSpecs.DEFAULT
, CookieSpecs.STANDARD
and CookieSpecs.STANDARD_STRICT
but none seem to work.
推荐答案
我已经在 4.3.6 和 4.5 版本中执行了示例代码.在 4.3.6 中,我使用了 RequestConfig.DEFAULT
并且它工作正常.使用 4.5 它返回
I have executed the example code with both versions 4.3.6 and 4.5 . With 4.3.6 I used RequestConfig.DEFAULT
and it was working fine. With 4.5 it returns
java.lang.NullPointerException: while trying to invoke the method java.lang.String.equalsIgnoreCase(java.lang.String) of a null object loaded from local variable 'domain'
at org.apache.http.impl.cookie.PublicSuffixDomainFilter.match(PublicSuffixDomainFilter.java:76)
at org.apache.http.impl.cookie.CookieSpecBase.match(CookieSpecBase.java:135)
at org.apache.http.impl.cookie.DefaultCookieSpec.match(DefaultCookieSpec.java:177)
at org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:165)
在 14 年 12 月 19 日晚上 10:59 添加了修订版 1646864 的更改:
The change was added with revision 1646864 on 12/19/14, 10:59 PM:
符合 RFC 6265 的 cookie 规范
RFC 6265 compliant cookie spec
为了使 4.5 版本正常工作,您需要将域设置为 cookie,并且域不等于确切的主机 org.apache.http.cookie.ClientCookie.DOMAIN_ATTR必须设置:
In order to make things work with version 4.5 you will need to set domain to the cookie and it the domain does not equal the exact host also org.apache.http.cookie.ClientCookie.DOMAIN_ATTR must be set:
BasicClientCookie cookie = new BasicClientCookie("cookieName", "cookieValue");
cookie.setDomain(".my.domain.com");
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
这篇关于Cookie 在 Apache httpclient 4.4 中被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!