Send aspx page from java

I need to send an aspx page from java. I am using HTTp Client as well as HttpUrlConnection for this. Calling the page is very simple, but I need to set the switch to check the status, and then set the value of the input field to what I'm looking for, and publish the page.

I used the post requestmethod for the HttpUrlConnection and tried to set the value of the input field with the value as an encoded string - I don't know if this is done correctly. Also I do not know how to set the state of the switch to a checkbox

So guys please help me how to do this.

any help would be much appreciated

thanks

Manozh

+6
java
source share
1 answer

You need to know the name of the input elements (including the submit button!). They should be sent as request parameters along with the desired value. You need to compose an HTTP request string based on these name-value pairs and write it to the request body.

Suppose the generated ASPX HTML is as follows:

<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> 

If you want to actually enter hello as the input value, select the second option option2 and press the submit button, then the final query line should look like this:

 foo=hello&bar=option2&action=send 

Write this in the request body. In the case of URLConnection this will 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:

+9
source share

All Articles