HtmlUnit – How to do a POST


I’ve just started using HtmlUnit (I also use Selenium but find it too slow for 90% of my testing).

HtmlUnit is generally easy to work with. Generally, it doesn’t need documentation as the Javadoc is useful and the methods are obvious. However, this is not the case for POST… so, here is how you do a POST 🙂

How to do a POST in HtmlUnit

There really isn’t much to it – you just need to know what code to use:

final WebClient webClient = new WebClient();

// Instead of requesting the page directly we create a WebRequestSettings object
WebRequestSettings requestSettings = new WebRequestSettings(
  new URL("URL GOES HERE"), HttpMethod.POST);

// Then we set the request parameters
requestSettings.setRequestParameters(new ArrayList());
requestSettings.getRequestParameters().add(new NameValuePair("name of value to post", "value"));

// Finally, we can get the page
HtmlPage page = webClient.getPage(requestSettings);

Quite a lot of work for a simple… I imagine it won’t be hard to wrap this up into a neat POST method.


7 Responses to HtmlUnit – How to do a POST

  1. Ahmed Ashour says:


    Thanks for your post.

    Yes, it is not easy, but also real browsers don’t provide a way for you to direct post a request, you have to go to a page, and click a link, then it will post.

    HtmlUnit main usage is simulating real browsers usage, and customized actions would need the user to dig into the API a little.

  2. colinhowe says:

    Very true! My problem was that I needed to test how Facebook notified my application when a user removed it… and Facebook does it via a POST.

    Great work on HtmlUnit 🙂

  3. LCamel says:

    If HtmlUnit can’t correctly generate a form with JavaScript, we may still need to do a direct POST…

    (Please remove my previous comment, thanks.)

    • lalith says:

      Htmlunit is working perfect with any jscript if you know how to handle them. search about synchronizing methods like (NicelyResynchronizingAjaxController) 🙂

  4. Hi my cherished one! I want to claim that this article is awesome, nice written and come with approximately all significant infos.
    I’d like to see extra posts of this nature .

  5. I have not checked in in charge of a little while as I think it’s time getting boring, the last few posts are great quality thus guess I am going to add you back in my daily bloglist. You deserve it friend 🙂

  6. ashes999 says:


    The last couple of comments on this post are spam.

    Also, could you please fix your CSS? The code sample truncates on the right-hand side. You can probably specify overflow or wrapping instead.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: