6

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

user503606
  • 63
  • 1
  • 3

1 Answers1

9

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:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Hi Balusc, String query = "ctl00$content_placeholder_body$BusinessSearch1$TextBox_NameSearch=C329164&ctl00$content_placeholder_body$BusinessSearch1$RadioButtonList_SearchType=Entity Number&ctl00$content_placeholder_body$BusinessSearch1$Button_Search=Search" called the page did not work,it simply show sthe contents of page I called – user503606 Nov 10 '10 at 19:42
  • You likely want to maintain the session cookie as well. This is often the case when the server side uses a component based MVC framework like ASP.NET-MVC or JSF. See the provided link for detail. – BalusC Nov 10 '10 at 20:33
  • I set the viewstate and used content type=application/x-www-form-urlencoded, it gives me error, but if I use content type text/html it works fine in the sense it shows the page but not th eresults i expect. I checked the headers and content type it uses is text/html, the form name="aspnetForm" method="post" action="cbs.aspx" onsubmit="javascript:return WebForm_OnSubmit. I want to give url of this site but I am afraid - can i email you at your email directly – user503606 Nov 10 '10 at 21:54
  • Did you maintain the session cooke? Anyway, install Firebug and check in the *Net* panel how the request body look like. You might have missed some crucial parameters. – BalusC Nov 10 '10 at 21:56
  • Cookie: __utma=176450042.2007764727.1289418280.1289418280.1289418280.1; __utmb=176450042.9.10.1289418280; __utmz=176450042.1289418280.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=176450042 Cache-Control: private when i use Set-Cookie string to check it is not there which means is it cookieless type of page,sorry I am bothering you so much – user503606 Nov 10 '10 at 22:30
  • So, there's no session cookie? Something like ASPSESSIONID? Those __utm cookies are just Google Analytics cookies. – BalusC Nov 10 '10 at 22:36
  • Oh I just solved the problem. I had one & missing between parameters. I guess it uses viewstate as somekind of sessionID. Now I just need to parse the results, do yu know of some good parsers – user503606 Nov 10 '10 at 22:52
  • Is it HTML? I recommend [Jsoup](http://jsoup.org). See also [Pros and cons of leading HTML parsers in Java](http://stackoverflow.com/questions/3152138/what-are-the-pros-and-cons-of-the-leading-java-html-parsers). Don't forget to mark the answer accepted if it helped in solving the problem :) – BalusC Nov 10 '10 at 22:53
  • There was another answer from other person and his suggestion also helped me a lot. I hope he sees this message – user503606 Nov 11 '10 at 23:23