用Java提交HTML表单 [英] Submit HTML Form in Java
问题描述
我试图填写HTML表单,按下表单的提交按钮,然后从中获得响应。
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.
我使用apache httpclient库。
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" />
推荐答案
你不用HttpClient ;它所做的只是HTTP东西,它与JS和DOM无关。
You don't "click a button" with HttpClient; all it does is HTTP stuff, which is unrelated to JS and the DOM.
如果您想模拟浏览器,请使用 JWebUnit ,它可以驱动HttpClient和Selenium,并提供JavaScript支持。
If you want to emulate a browser, use something like JWebUnit, which can drive both HttpClient and Selenium, and provides JavaScript support.
这篇关于用Java提交HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!