欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HttpClient 报错 Invalid cookie header, Invalid 'expires' attribute: Thu, 01 Jan 1970 00:00:00 GMT

程序员文章站 2023-02-10 22:08:43
今天在给我姐夫写一个 QQ 自动加好友的程序,但是在 HttpClient 登陆 QQ 的时候报了一个错: 这里提示设置的 Cookie expires 无效,我看了下值都是长这样的 Thu, 01 Jan 1970 00:00:00 GMT。查了下资料发现说是因为 HttpClient4 默认的 ......

今天在给我姐夫写一个 qq 自动加好友的程序,但是在 httpclient 登陆 qq 的时候报了一个错:

2019-02-12 14:08:33.727 [thread-3] - invalid cookie header: "set-cookie: pt2gguin=;expires=thu, 01 jan 1970 00:00:00 gmt;path=/;domain=qq.com;". invalid 'expires' attribute: thu, 01 jan 1970 00:00:00 gmt
2019-02-12 14:08:33.738 [thread-3] - invalid cookie header: "set-cookie: pt2gguin=o3413198711;expires=tue, 19 jan 2038 03:14:07 gmt;path=/;domain=ptlogin2.qq.com;". invalid 'expires' attribute: tue, 19 jan 2038 03:14:07 gmt
2019-02-12 14:08:33.751 [thread-3] - invalid cookie header: "set-cookie: pt_recent_uins=813d3b6002b7444a612a73b16d2bfc8d63cf29eee15b914f23387c79f948909fa0d856264f22e6d89bc1722e84d35c466e84178100c282cb;expires=thu, 14 mar 2019 06:08:34 gmt;path=/;domain=ptlogin2.qq.com;httponly;". invalid 'expires' attribute: thu, 14 mar 2019 06:08:34 gmt
2019-02-12 14:08:33.760 [thread-3] - invalid cookie header: "set-cookie: rk=jyxghgen/q;expires=tue, 19 jan 2038 03:14:07 gmt;path=/;domain=qq.com;". invalid 'expires' attribute: tue, 19 jan 2038 03:14:07 gmt
2019-02-12 14:08:33.772 [thread-3] - invalid cookie header: "set-cookie: ptcz=c22602e5a8b3b23af9924e4fe230dca938964da3a2536767863084df7c79ffdd;expires=tue, 19 jan 2038 03:14:07 gmt;path=/;domain=qq.com;". invalid 'expires' attribute: tue, 19 jan 2038 03:14:07 gmt
2019-02-12 14:08:33.783 [thread-3] - invalid cookie header: "set-cookie: ptcz=;expires=thu, 01 jan 1970 00:00:00 gmt;path=/;domain=ptlogin2.qq.com;". invalid 'expires' attribute: thu, 01 jan 1970 00:00:00 gmt
2019-02-12 14:08:33.793 [thread-3] - invalid cookie header: "set-cookie: airkey=;expires=thu, 01 jan 1970 00:00:00 gmt;path=/;domain=qq.com;". invalid 'expires' attribute: thu, 01 jan 1970 00:00:00 gmt

这里提示设置的 cookie expires 无效,我看了下值都是长这样的 thu, 01 jan 1970 00:00:00 gmt。查了下资料发现说是因为 httpclient4 默认的 cookie 策略是不支持这种格式。

既然查出原因就好办了

1、第一种办法就是把服务器设置的 cookie 值改掉

2、修改默认的 cookie 策略,改为 standard_strict 或者 standard

我这里因为是腾讯的服务器我改不了所有我选择了第二种:

我这里也不是直接用的 httpclient4,用的是 net.dongliu.requests 封装好的,我也没找到他有提供修改的 cookie 策略的地方。我比较粗暴,直接把他封装的clientbuilder 类修改成 cookiespecs.standard_stric 直接类覆盖了。

 requestconfig.builder configbuilder = requestconfig.custom()
              .setconnecttimeout(connecttimeout)
              .setsockettimeout(sockettimeout)
              // we use connect timeout for connection request timeout
              .setconnectionrequesttimeout(connecttimeout)
              .setcookiespec(cookiespecs.standard);

 

或者直接

httpclient httpclient = httpclients.custom()
       .setdefaultrequestconfig(requestconfig.custom()
       .setcookiespec(cookiespecs.standard).build())
       .build();

 

也有说可以用 browser_compatibility 或者 netscape  策略的,但是我试了没有成功。

 

相关资料链接:

https://issues.apache.org/jira/browse/httpclient-1077