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

-- ::33.727 [Thread-] - Invalid cookie header: "Set-Cookie: pt2gguin=;Expires=Thu, 01 Jan 1970 00:00:00 GMT;Path=/;Domain=qq.com;". Invalid 'expires' attribute: Thu,  Jan  :: GMT
-- ::33.738 [Thread-] - 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, Jan :: GMT
-- ::33.751 [Thread-] - 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, Mar :: GMT
-- ::33.760 [Thread-] - 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, Jan :: GMT
-- ::33.772 [Thread-] - Invalid cookie header: "Set-Cookie: ptcz=c22602e5a8b3b23af9924e4fe230dca938964da3a2536767863084df7c79ffdd;Expires=Tue, 19 Jan 2038 03:14:07 GMT;Path=/;Domain=qq.com;". Invalid 'expires' attribute: Tue, Jan :: GMT
-- ::33.783 [Thread-] - Invalid cookie header: "Set-Cookie: ptcz=;Expires=Thu, 01 Jan 1970 00:00:00 GMT;Path=/;Domain=ptlogin2.qq.com;". Invalid 'expires' attribute: Thu, Jan :: GMT
-- ::33.793 [Thread-] - Invalid cookie header: "Set-Cookie: airkey=;Expires=Thu, 01 Jan 1970 00:00:00 GMT;Path=/;Domain=qq.com;". Invalid 'expires' attribute: Thu, Jan :: 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://stackoverflow.com/questions/36473478/fixing-httpclient-warning-invalid-expires-attribute-using-fluent-api

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

HttpClient 报错 Invalid cookie header, Invalid 'expires' attribute: Thu, 01 Jan 1970 00:00:00 GMT的更多相关文章

  1. HttpClient4 警告: Invalid cookie header 的问题解决(转)

    原文地址:HttpClient4 警告: Invalid cookie header 的问题解决 最近使用HttpClient4的时候出现如下警告信息 org.apache.http.client.p ...

  2. webMagic解析淘宝cookie 提示Invalid cookie header

    webMagic解析淘宝cookie 提示Invalid cookie header 在使用webMagic框架做爬虫爬取淘宝极又家页面时候一直提醒cookie设置不可用如下图 淘宝的验证特别严重,c ...

  3. 使用xadmin搜索search_fields报错:Related Field got invalid lookup: icontains

    一.问题描述 使用xadmin实现Django后台功能时,使用search_fields = [字段名,字段名],在搜索的时候报错Related Field got invalid lookup: i ...

  4. Vue报错之"[Vue warn]: Invalid prop: type check failed for prop "jingzinum". Expected Number with value NaN, got String with value "fuNum"."

    一.报错截图 [Vue warn]: Invalid prop: type check failed for prop "jingzinum". Expected Number w ...

  5. EL表达式报错:  According to TLD or attribute directive in tag file, attribute value does not accept any expressions

    EL表达式报错: According to TLD or attribute directive in tag file, attribute value does not accept any ex ...

  6. pymysql下报错:numpy.float64 object has no attribute 'translate' 可能是pandas版本的问题

    pymysql下报错:numpy.float64 object has no attribute 'translate'.定位到db.merge函数中,dataframe中浮点型元素的类型为numpy ...

  7. 使用jstl报错:According to TLD or attribute directive in tag file, attribute value does not accept any expressions

    使用jstl报错:According to TLD or attribute directive in tag file, attribute value does not accept any ex ...

  8. 使用jstl标签报错:According to TLD or attribute directive in tag file, attribute value

    原来jstl标签版本不一样,标签支持不一样. jstl1.0标签库不支持表达式,如: <c:if test="${query01 == null}">   <js ...

  9. pycharm安装 package报错:module 'pip' has no attribute 'main'

    转自: <pycharm安装 package报错:module 'pip' has no attribute 'main'> https://www.cnblogs.com/Fordest ...

随机推荐

  1. 卷积神经网络之LeNet

    开局一张图,内容全靠编. 上图引用自 [卷积神经网络-进化史]从LeNet到AlexNet. 目前常用的卷积神经网络 深度学习现在是百花齐放,各种网络结构层出不穷,计划梳理下各个常用的卷积神经网络结构 ...

  2. 一、Java 23 种设计模式简介

    一.23种设计模式分类: 二.设计模式的六大原则: 1.开闭原则(Open Close Principle):对扩展开放,对修改关闭.在程序需要进行拓展的时候,不能去修改原有的代码,实现一个热插拔的效 ...

  3. 前端css

    CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...

  4. CODING 研发管理系统上线全球加速,助力企业跨区域协作

    CODING 研发管理系统现已全面支持全类型代码仓库的 全球加速访问. 随着国内互联网红利的日趋枯竭与全球互联网的加速普及.越来越多的企业开始走出国门,将目光投向全世界,搭建跨国体系.跨出国门的中国企 ...

  5. 万马齐喑究可哀-中文编程的又一波"讨论"

    刚申诉了自动折叠, 还是把回答转帖一下: 吴烜:假设中国人最先开发电脑和设计程序语言,那么各种程序语言会使用汉字吗? 这种有明显倾向性的问题怎么还有市场呢...不管谁先开发的电脑(就不论算盘之类是不是 ...

  6. shell 简单脚本编程

    shell脚本编程 编译器,解释器 编程语言: 机器语言,汇编语言,高级语言 静态语言:编译型语言 强类型(变量) 事先转换成可执行格式 C,C++,JAVA,C# 动态语言:解释型语言,on the ...

  7. 死磕 java集合之TreeSet源码分析

    问题 (1)TreeSet真的是使用TreeMap来存储元素的吗? (2)TreeSet是有序的吗? (3)TreeSet和LinkedHashSet有何不同? 简介 TreeSet底层是采用Tree ...

  8. [区块链] 带你进入Bitcoin开发 - 环境搭建

    学习完区块链枯燥乏味的.高深的.必备的基础知识后,终于可以走上开发之路了!真是迫不及待啦!之后博客更新主要放在区块链的开发上,相信这才是大多数同学更加感兴趣的地方!学习过程从最经典的区块链鼻祖-比特币 ...

  9. HandlerInterceptor里@Autowired对象为空的解决方法

    That's because Spring isn't managing your PagePopulationInterceptor instance. You are creating it yo ...

  10. Python:tesserocr 在 windows 下的安装及简单使用

    tesserocr 是 python 的一个 OCR 库,它是对 tesseract 做的一层 Python API 封装,所以他的核心是tesseract. tesseract 的安装见 https ...