You need to know the name of the input elements (including the submit button itself!). They needs to be sent as request parameters together with the desired value. You need to compose a HTTP query string based on those name-value pairs and write it to the request body.
Assume that the generated HTML of the ASPX page look like:
<form action="page.aspx" method="post">
<input type="text" name="foo" />
<input type="radio" name="bar" value="option1" />
<input type="radio" name="bar" value="option2" />
<input type="radio" name="bar" value="option3" />
<input type="submit" name="action" value="send" />
</form>
When you want to virtually enter hello
as input value, select the 2nd option option2
and press the submit button, then the final query string needs to look like this:
foo=hello&bar=option2&action=send
Write that to the request body. In case of URLConnection
, it would be:
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));
See also: