我正在寻找一种使用 HTMLUnit 接受 cookie(所有 cookie)的方法
我正在尝试使用 HTMLUnit 登录我的 wordpress 网站,但是当 cookie 被拒绝时,我无法提交表单(因此无法登录),因为我的错误是:
2012 年 7 月 14 日晚上 10:42:24 org.apache.http.client.protocol.ResponseProcessCookies processCookies 警告:Cookie 被拒绝:
代码:
package backend;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Set;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.CookieManager;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;
public class temp {
WebClient myClient = new WebClient(BrowserVersion.FIREFOX_3);
CookieManager cm = new CookieManager();
HtmlPage page;
public void Someting() throws FailingHttpStatusCodeException, MalformedURLException, IOException{
//Disabling Javascript for now.
myClient.setJavaScriptEnabled(false);
myClient.setCssEnabled(false);
myClient.setCookieManager(cm);
page = myClient.getPage("http://nick.wordpress.com/wp-admin");
Set<Cookie> cookies = myClient.getCookieManager().getCookies();
System.out.println("Page status code: " + page.getWebResponse().getStatusCode() + "\nPage status message: " + page.getWebResponse().getStatusMessage());
if(cookies != null)
{
for(Cookie cookie : cookies)
{
this.myClient.getCookieManager().addCookie(cookie);
}
}
System.out.println("We have: " + myClient.getCookieManager().getCookies().size() + " cookie");
}
public static void main(String args[]) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
temp t = new temp();
t.Someting();
}
}
此代码不包括 WordPress 表单填写/提交功能,因为这似乎不相关。
在网上搜索时,我只能找到 1 或 2 个模糊的答案,我应该修改"http.client.protocol.ResponseProcessCookies"
课程,但我希望没有这个也可以完成。