我正在寻找一种使用HTMLUnit接受cookie(所有cookie)的方法
我正在尝试使用HTMLUnit登录我的wordpress网站,但是当我的错误被拒绝时,我无法提交表单(因此无法登录):
2012年7月14日下午10:42:24 org.apache.http.client.protocol.ResponseProcessCookies processCookies 警告:Cookie被拒绝:
CODE:
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个vaugue答案,我应该修改“http.client.protocol.ResponseProcessCookies”类,但我希望没有这个可以完成。
答案 0 :(得分:0)
您的问题已解答here
您必须重新创建CookieManager类。
找到方法public synchronized Set<Cookie> getCookies(final URL url)
并删除语句if (spec.match(cookie, cookieOrigin))
。