如何强制 Java 的 HttpClient 接受无效的 cookie?
我正在编写一个登录 WordPress 的脚本,但是,HttpClient 的 cookie 策略将 WordPress 设置的 cookie 识别为无效:
May 17, 2009 12:07:43 PM org.apache.commons.httpclient.HttpMethodBase processCookieHeaders
WARNING: Cookie rejected: "$Version=0; wordpress_dce2080bc042b2e639e4f5b3b704aa43=admin%7C1243786064%7C4c56aef46b1210d3d43d8b829fdf4d9a; $Path=/wp-content/plugins". Illegal path attribute "/wp-content/plugins". Path of origin: "/wp-login.php"
May 17, 2009 12:07:43 PM org.apache.commons.httpclient.HttpMethodBase processCookieHeaders
WARNING: Cookie rejected: "$Version=0; wordpress_dce2080bc042b2e639e4f5b3b704aa43=admin%7C1243786064%7C4c56aef46b1210d3d43d8b829fdf4d9a; $Path=/wp-admin". Illegal path attribute "/wp-admin". Path of origin: "/wp-login.php"
没有 cookie 支持,脚本无法登录。 我该如何解决这个问题?
(使用HttpClient 3.1)
I'm working on a script that logs into WordPress, however, HttpClient's cookie policy identifies the cookies set by WordPress to be invalid:
May 17, 2009 12:07:43 PM org.apache.commons.httpclient.HttpMethodBase processCookieHeaders
WARNING: Cookie rejected: "$Version=0; wordpress_dce2080bc042b2e639e4f5b3b704aa43=admin%7C1243786064%7C4c56aef46b1210d3d43d8b829fdf4d9a; $Path=/wp-content/plugins". Illegal path attribute "/wp-content/plugins". Path of origin: "/wp-login.php"
May 17, 2009 12:07:43 PM org.apache.commons.httpclient.HttpMethodBase processCookieHeaders
WARNING: Cookie rejected: "$Version=0; wordpress_dce2080bc042b2e639e4f5b3b704aa43=admin%7C1243786064%7C4c56aef46b1210d3d43d8b829fdf4d9a; $Path=/wp-admin". Illegal path attribute "/wp-admin". Path of origin: "/wp-login.php"
Without cookie support, the script can't login.
How can I get around this?
(Using HttpClient 3.1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
检查这篇文章以获取更多提示:
Java HtmlUnit - 无法登录到 wordpress
在类似的情况下,我能够使用以下代码(尽管我承认它在特定的 WordPress 情况下不起作用):
Check this post for further hints:
Java HtmlUnit - can't login to wordpress
In similar situations I was able to use the following code (although I admit it doesn't work in the specific wordpress case):
HttpClient 是世界上唯一尊重 RFC 的东西。 我首先提出了删除整个前缀路径的请求。 有很多网站将路径设置为然后将您转发到的路径。 这是一件非常有用的事情。 由于 Chrome、IE、Firefox、Safari、Opera 和其他所有浏览器都不关心,我认为 HTTPClient 应该停止关心,这样我们就可以继续我们的生活。
HttpClient is the only thing in the world that respects the RFC. I put in a request to strike the whole prefix path to begin with. There are lots of sites that set the path to a path they then forward you to. It is a very useful thing to do. Since Chrome, IE, Firefox, Safari, Opera and every other browser doesn't care I think HTTPClient should stop caring so we can move on with our lives.
看一下这个页面,HttpClient Cookie 指南,看看是否调整 cookie 策略可以解决您的问题。
编辑:
更具体地说,尝试 Netscape Draft 策略并查看其宽松的限制是否适用于 WordPress cookie。
Take a look at this page, HttpClient Cookie Guide, and see if adjusting the cookie policy fixes your problem.
EDIT:
More specifically, try the Netscape Draft policy and see if its relaxed restrictions work with the WordPress cookies.
您能否注册自己更宽松的 Cookie 政策?
Can you register your own Cookie Policy that is more relaxed?
您可以实施自己的 cookie 策略。
请参阅此HTTPClient 指南中的第 3.6 章。
You can implement your own cookie policy.
See chapter 3.6 in this HTTPClient guide.
我认为,你可以下载httpclient的源代码,找到log.warn ...的位置,可以删除它们。 我就这么做了。
In my opinion,you can download the source code of the httpclient, find the location of the log.warn ... , can delete them. I did so.