Submit HTML Form in Java

2019-02-14 08:24发布

问题:

I am trying to fill in a HTML Form, press a submit button of the form and then get the response from it.

Filling in the form works really well but i can't figure out how to press the submit button on the page.

I am using the apache httpclient libraries.

My code is:

        httpclient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost(pUrl);

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("filter_response_time_http", "1"));
        nvps.add(new BasicNameValuePair("filter_port", "80"));
        nvps.add(new BasicNameValuePair("filter_country", "US"));
        nvps.add(new BasicNameValuePair("submit", "Anzeigen"));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpost);


        entity = response.getEntity();

The code for the submit button is:

<input onclick="doSubmit();" id="submit" type="submit" value="Anzeigen" name="submit" /> 

回答1:

You don't "click a button" with HttpClient; all it does is HTTP stuff, which is unrelated to JS and the DOM.

If you want to emulate a browser, use something like JWebUnit, which can drive both HttpClient and Selenium, and provides JavaScript support.



回答2:

Your submit button calls a javascript function when clicked. You can't emulate that behaviour using your java code.

For that you need to use a headless browser like htmlunit which can handle javascript.