Java HtmlUnit - 无法登录到 wordpress [英] Java HtmlUnit - can't login to wordpress
问题描述
我正在尝试使用 HtmlUnit 登录到我的本地 wordpress 网站,但它似乎存在 cookie 问题.
I'm trying to use HtmlUnit to login to my local wordpress website but it seems to have a cookies issue.
这就是代码的开头:
WebClient webClient = new WebClient();
HtmlPage loginPage = webClient.getPage("http://localhost/flowersWp/wp-admin");
HtmlForm form = loginPage.getFormByName("loginform");
这就是我在日志中得到的.有人有想法吗?谢谢.
That's what I get in the log. Anyone has an idea? Thanks.
2010 年 11 月 27 日下午 12:43:35org.apache.http.client.protocol.ResponseProcessCookies processCookies警告:Cookie 被拒绝:[版本:0][名称:wordpress_2418eeb845ebfb96f6f1a71ab8c5625a][值:+][域:本地主机][路径:/flowersWp/wp-admin][到期:11 月 27 日星期五 12:43:35 IST2009]".非法路径属性/flowersWp/wp-admin".来源路径:"/flowersWp/wp-login.php"
Nov 27, 2010 12:43:35 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies WARNING: Cookie rejected: "[version: 0][name: wordpress_2418eeb845ebfb96f6f1a71ab8c5625a][value: +][domain: localhost][path: /flowersWp/wp-admin][expiry: Fri Nov 27 12:43:35 IST 2009]". Illegal path attribute "/flowersWp/wp-admin". Path of origin: "/flowersWp/wp-login.php"
推荐答案
WebClient 使用的是 apache httpclient,所以是 HttpClient 的问题.
WebClient is using apache httpclient, so it is an HttpClient problem.
根据我的经验,这与重定向有关.我已经使用 HttpClient 解决了这个问题并注册了我自己的 cookie 支持:
From my experience, it has to do with redirections. I've gotten rid of this problem using HttpClient and registering my own cookie support:
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Bind custom cookie store to the local context
httpclient.setCookieStore(cookieStore);
CookieSpecFactory csf = new CookieSpecFactory() {
public CookieSpec newInstance(HttpParams params) {
return new BrowserCompatSpec() {
@Override
public void validate(Cookie cookie, CookieOrigin origin)
throws MalformedCookieException {
// Oh, I am easy.
// Allow all cookies
log.debug("custom validate");
}
};
}
};
httpclient.getCookieSpecs().register("easy", csf);
httpclient.getParams().setParameter(
ClientPNames.COOKIE_POLICY, "easy");
好吧,在 HtmlUnit 中,我无法直接访问 httpclient,但我正在考虑更改其源代码以这样做,因为我需要使用 JavaScript 支持连接到 wordpress.
Well, in HtmlUnit I have no direct access to httpclient, but I'm thinking of changing its source code to do so, as I need to connect to wordpress with JavaScript support.
这篇关于Java HtmlUnit - 无法登录到 wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!