如何强制 Java 的 HttpClient 接受无效的 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/874903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:10:44  来源:igfitidea点击:

How can I force Java's HttpClient to accept invalid cookies?

javawordpresscookieshttpclient

提问by Alex

I'm working on a script that logs into WordPress, however, HttpClient's cookie policy identifies the cookies set by WordPress to be invalid:

我正在编写一个登录 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"

Without cookie support, the script can't login. How can I get around this?

如果没有 cookie 支持,脚本将无法登录。我怎样才能解决这个问题?

(Using HttpClient 3.1)

(使用 HttpClient 3.1)

回答by kongo09

Check this post for further hints: Java HtmlUnit - can't login to wordpress

查看此帖子以获取更多提示: Java HtmlUnit - 无法登录 wordpress

In similar situations I was able to use the following code (although I admit it doesn't work in the specific wordpress case):

在类似的情况下,我能够使用以下代码(尽管我承认它在特定的 wordpress 情况下不起作用):

HttpClient client = new HttpClient();
client.getParams().setParameter("http.protocol.single-cookie-header", true);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

回答by Eric Twilegar

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 是世界上唯一尊重 RFC 的东西。我提出了一个请求,以开始攻击整个前缀路径。有很多站点将路径设置为他们然后将您转发到的路径。这是一件非常有用的事情。由于 Chrome、IE、Firefox、Safari、Opera 和所有其他浏览器都不关心我认为 HTTPClient 应该停止关心,这样我们就可以继续我们的生活。

回答by Jason Fritcher

Take a look at this page, HttpClient Cookie Guide, and see if adjusting the cookie policy fixes your problem.

看看这个页面,HttpClient Cookie 指南,看看调整 cookie 策略是否能解决您的问题。

EDIT:

编辑:

More specifically, try the Netscape Draft policy and see if its relaxed restrictions work with the WordPress cookies.

更具体地说,试试 Netscape Draft 策略,看看它的宽松限制是否适用于 WordPress cookie。

回答by Kevin

Can you register your own Cookie Policy that is more relaxed?

可以注册自己更宽松的Cookie政策吗?

回答by RoyBass

You can implement your own cookie policy.
See chapter 3.6 in this HTTPClient guide.

您可以实施自己的 cookie 政策。
请参阅此HTTPClient 指南中的第 3.6 章。

回答by wangzhiju

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.

在我看来,你可以下载httpclient的源代码,找到log.warn ...的位置,可以删除它们。我这样做了。