我想填写一个HTML表单,按下表单的提交按钮,然后得到它的响应。在Java中提交HTML表单
填写表单的效果非常好,但我无法弄清楚如何按下页面上的提交按钮。
我使用的是Apache httpclient库。
我的代码是:
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();
提交按钮的代码为:
<input onclick="doSubmit();" id="submit" type="submit" value="Anzeigen" name="submit" />
'UrlEncodedFormEntity'的第二个参数应该是'java.nio.charset.StandardCharsets.UTF_8',因为'HTTP.UTF_8'已被弃用。 – 0x6B6F77616C74 2013-07-06 19:36:14