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:35 org.apache.http.client.protocol.ResponseProcessCookies processCookies 警告:Cookie被拒绝:"[版本:0] [名称: wordpress_2418eeb845ebfb96f6f1a71ab8c5625a] [值:+] [域: 本地主机] [路径:/flowersWp/wp-admin] [到期日:IST 11月27日12:43:35 2009]".非法路径属性"/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屋!