最近在使用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将会被忽略