Submit html form programmatically in android

I would like to submit a form programmatically in android. I do not want the user to interact with the web browser. The user will provide the input to EditField, and then the inputs will be sent via the HTTP mail method via HTTPwebmethod. But I did not succeed in the same. Please advise. I used HTMLUnit in java but did not work in android.

final WebClient webClient = new WebClient(); final HtmlPage page1 = webClient.getPage("http://www.mail.example.com"); final HtmlForm form = page1.getHtmlElementById("loginform"); final HtmlSubmitInput button = form.getInputByName("btrn"); final HtmlTextInput textField1 = form.getElementById("user"); final HtmlPasswordInput textField2 = form.getElementById("password");textField1.setValueAttribute("user.name"); textField2.setValueAttribute("pass.word"); final HtmlPage page2 = button.click(); 
+4
source share
1 answer

Unfortunately. I'm sorry. It looks like you are trying to do POST through a browser.

Here is a snippet that I used to perform HTTP POST on Android without going through a web browser:

 HttpClient httpClient = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS); HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS); HttpPost httpPost = new HttpPost(url); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("name1", "value1")); nameValuePairs.add(new BasicNameValuePair("name2", "value2")); nameValuePairs.add(new BasicNameValuePair("name3", "value3")); // etc... httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpClient.execute(httpPost); 

I think this should work on what you are trying to do. I have TIMEOUT_MS set to 10000 (so, 10 seconds)

Then you can read the server response using something like this:

 BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 8096); 
+11
source

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


All Articles