您需要知道输入元素的名称(包括提交按钮本身!).它们需要作为请求参数与期望值一起发送.您需要根据这些名称 – 值对组合HTTP查询字符串,并将其写入请求正文.
假设生成的ASPX页面的HTML如下所示:
当您想要虚拟输入hello作为输入值时,选择第二个选项option2并按下提交按钮,然后最终查询字符串需要如下所示:
foo=hello&bar=option2&action=send
将其写入请求正文.在URLConnection的情况下,它将是:
String query = "foo=hello&bar=option2&action=send";
String charset = "UTF-8";
URLConnection connection = new URL("http://example.com/page.aspx").openConnection();
connection.setDoOutput(true); // Triggers POST method.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
connection.getOutputStream().write(query.getBytes(charset));
也可以看看: