I'm looking for a way to accept cookies (all cookies) using HTMLUnit
I'm trying to login to my wordpress site using HTMLUnit but I can't submit the form (and therefor can not login) when the cookies are rejected as my error is:
Jul 14, 2012 10:42:24 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies WARNING: Cookie rejected:
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();
}
}
This code is excluding the WordPress form filling/submitting function as this doesn't seem relevant.
When searching the web I could only find 1 or 2 vague answers where I should modify the "http.client.protocol.ResponseProcessCookies"
class but I hope it can be done without this.