最近在使用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.
6265, section 4 intended for interoperability with existing servers that do not conform to the well
behaved profile.
by Netscape Communications. It should be avoided unless absolutely necessary for compatibility
with legacy code.
specification defined by RFC 2965. Please do not use in new applications.--->已廢棄
specification defined by RFC 2109. Please do not use in new applications--->已廢棄
older versions of browser applications such as Microsoft Internet Explorer and Mozilla FireFox.
Please do not use in new applications.--->已廢棄
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將會被忽略