How can I force Java's HttpClient to accept in

2020-02-09 05:53发布

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)

6条回答
来,给爷笑一个
2楼-- · 2020-02-09 06:07

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

查看更多
太酷不给撩
3楼-- · 2020-02-09 06:07

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.

查看更多
Anthone
4楼-- · 2020-02-09 06:09

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.

查看更多
淡お忘
5楼-- · 2020-02-09 06:10

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

查看更多
Juvenile、少年°
6楼-- · 2020-02-09 06:20

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 client = new HttpClient();
client.getParams().setParameter("http.protocol.single-cookie-header", true);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
查看更多
做自己的国王
7楼-- · 2020-02-09 06:21

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.

查看更多
登录 后发表回答