编程的机器人提交HTML表单 [英] submit html form programmatically in android

查看:271
本文介绍了编程的机器人提交HTML表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android的编程方式提交表单。我不想用Web浏览器的任何用户交互。用户将提供一个EditField中输入,然后输入将通过HTTPwebmethod提交通过HTTP POST方法。但我没有得到任何相同的成功。请指教。我已经使用在的HtmlUnit的Java,但它不工作的机器人。

 最后的Web客户端Web客户端=新的Web客户端();
 最后HtmlPage第1页= webClient.getPage(http://www.mail.example.com);
     最终的HtmlForm形式= page1.getHtmlElementById(登录表单);

    最后HtmlSubmitInput按钮= form.getInputByName(btrn);
    最后HtmlTextInput textField1的= form.getElementById(用户);
   最后HtmlPasswordInput textField2 = form.getElementById(密码); textField1.setValueAttribute(user.name);
    textField2.setValueAttribute(pass.word);最后HtmlPage第2页= button.click();
 

解决方案

抱歉。抱歉。看起来,你是想通过浏览器POST毕竟。

这是我一直在使用,以实现HTTP POST的Andr​​oid中,而无需通过网络浏览器的一个片段:

  HttpClient的HttpClient的=新DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(),TIMEOUT_MS);
HttpPost httpPost =新HttpPost(URL);
名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
nameValuePairs.add(新BasicNameValuePair(名1,值1));
nameValuePairs.add(新BasicNameValuePair(名2,值2));
nameValuePairs.add(新BasicNameValuePair(NAME3,值3));
// 等等...
httpPost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpClient.execute(httpPost);
 

我觉得应该为你想要做的事。我已TIMEOUT_MS设置为10000(因此,10秒)

然后就可以读出服务器的响应使用是这样的:

 的BufferedReader BR =新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()),8096。);
 

I would like to submit a form programmatically in android. I don't want any user interaction with a web browser. The user will provide inputs in an EditField and then the inputs will be submitted Through http post method via HTTPwebmethod. But I didn't get any success in the same. Please advise. I have used HTMLUnit in java but its not working 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();

解决方案

Oops. Sorry. Looks like you are trying to POST through the browser afterall.

Here's a snippet I've been using to accomplish HTTP POST's in Android without going through the 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 that should work for what you're trying to do. I have TIMEOUT_MS set to 10000 (so, 10 seconds)

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

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

这篇关于编程的机器人提交HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆