HttpClient4.5.3 警告:Invalid cookie header 的問題解決

最近在使用HttpClient發送請求時發現經常報Invalid cookie header警告

org.apache.http.client.protocol.ResponseProcessCookies processCookies
警告: Invalid cookie header: "Set-Cookie: q_c1=484d1c0f82e1461790775962c93f1875|1516253307000|1516253307000; path=/; expires=Sun, 17 Jan 2021 05:28:27 GMT; domain=zhihu.com; httponly". Invalid 'expires' attribute: Sun, 17 Jan 2021 05:28:27 GMT

這時候需要修改請求中的cookie策略(CookieSpecs)

RequestConfig defaultConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
getMethod.setConfig(defaultConfig);
這樣即可以解決


附帶幾種CookieSpecs的詳解

HttpClient附帶了幾個CookieSpec的實現

Standard strict:State management policy compliant with the syntax and semantics of the wellbehaved profile defined by RFC 6265, section 4.


Standard:State management policy compliant with a more relaxed profile defined by RFC
6265, section 4 intended for interoperability with existing servers that do not conform to the well

behaved profile.


Netscape draft (obsolete):This policy conforms to the original draft specification published
by Netscape Communications. It should be avoided unless absolutely necessary for compatibility

with legacy code.


RFC 2965 (obsolete):State management policy compliant with the obsolete state management

specification defined by RFC 2965. Please do not use in new applications.--->已廢棄


RFC 2109 (obsolete):State management policy compliant with the obsolete state management

specification defined by RFC 2109. Please do not use in new applications--->已廢棄


Browser compatibility (obsolete):This policy strives to closely mimic the (mis)behavior of
older versions of browser applications such as Microsoft Internet Explorer and Mozilla FireFox.

Please do not use in new applications.--->已廢棄


Default:Default cookie policy is a synthetic policy that picks up either RFC 2965, RFC 2109
or Netscape draft compliant implementation based on properties of cookies sent with the HTTP
response (such as version attribute, now obsolete). This policy will be deprecated in favor of the
standard (RFC 6265 compliant) implementation in the next minor release of HttpClient.


Ignore cookies:所有的cookie將會被忽略


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章