views:

700

answers:

3

I want to fill a text field of a HTTP form through java and then want to click on the submit button through java so as to get the page source of the document returned after submitting the form. I can do this by sending HTTP request directly but I don't to this in this way.

Very Urgent. You can also paste the code or demo code to explain in a better way.

A: 

You would probably need to write a Java Applet, as the only other way than sending a direct request would be to have it interface with the browser.

Of course, for this to work, you would have to embed the applet in the page. If you don't control the page, this can't be done. If you do control the page, you might as well be using Javascript, instead of trying to get a Java Applet to do it, which would be much more cumbersome and difficult.

Just to clarify, what is the problem you are having creating an HTTP Request and why do you want to use a different method?

Kibbee
+2  A: 

I usually do it using HtmlUnit. They have an example on their page :

@Test
public void submittingForm() throws Exception {
    final WebClient webClient = new WebClient();

    // Get the first page
    final HtmlPage page1 = webClient.getPage("http://some_url");

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change.
    final HtmlForm form = page1.getFormByName("myform");

    final HtmlSubmitInput button = form.getInputByName("submitbutton");
    final HtmlTextInput textField = form.getInputByName("userid");

    // Change the value of the text field
    textField.setValueAttribute("root");

    // Now submit the form by clicking the button and get back the second page.
    final HtmlPage page2 = button.click();
}

And you can read more here.

Geo
+2  A: 

If you don't want to talk HTTP directly (why?), then take a look at Watij.

It allows you to invoke a browser (IE) as a COM control within your Java process, navigate through page elements by using their document ids etc., fill in forms and press buttons. Because it's running a browser, Javascript will run as normal (like if you were doing this manually).

Brian Agnew
A lot of thanks. I was just looking the same option and found it as the best option that suits my need.
Yatendra Goel
In your other post you mention linux. Not sure if it's the same app, but Watij is Windows only.
Wayne Young
Yes it is the same App. I need to run the app on linux (debian). Can you suggest me the same thing like watij for linux.It's urgent.Second problem, com/jniwrapper/win32/ie/WebBrowser class not found..From where can I download that library
Yatendra Goel