Submit HTML form in Java

I am trying to fill out an HTML form, click the submit button and get a response from it.

Filling out the form works very well, but I can't figure out how to click the submit button on the page.

I am using 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(); 

Submit Button Code:

 <input onclick="doSubmit();" id="submit" type="submit" value="Anzeigen" name="submit" /> 
+4
source share
2 answers

You do not click the "HttpClient" button; all he does is HTTP stuff that is not JS and DOM related.

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

+3
source

The submit button calls the javascript function when clicked. You cannot emulate this behavior using your Java code.

To do this, you need to use a mute browser such as htmlunit , which can handle javascript.

+1
source

Source: https://habr.com/ru/post/1393555/


All Articles