I need to submit an aspx page from java. I am using HTTp Client and as well as HttpUrlConnection to do so. Calling the page is easy, but i need to set a radio button to checked state and then set the value of input field to what I am searching for and post the page.
I used post requestmethod on HttpUrlConnection and tried to set the value of input field with the value as encoded string - don't know if it is the right way to do so. Also I don't know how to set the radio button state to checked
So can you guys please help me how to do this task.
any help will be highly appreciated
Thanks
Manoj
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));
URLConnection
to fire and handle HTTP requests?If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With