Question

I want to make an application (in .Net) that fills and submits a form (in an asp.net website).

This applications should read the page, find the fields (inputs), extract name/id of the fields I want to fill it in and submit the page to the server.

I don't want an app that holds an webbrowser control and automate the navigation on it!

What I have: I have the part that download the html, I have the part that finds the fields and extract their names/ids.

What I need: A way to submit the form to the server (POST, not GET).

On the html of the page the submission is done by javascript, something like this:

javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Button1", "", true, "", "", false, false))

The question is: how to submit in this case?

Was it helpful?

Solution

You can use the HttpWebRequest/HttpWebResponse objects to send/receive HTTP requests to a server. When you get the response, look for the various INPUT fields that you want to modify and build a POST request data block with the various fields, like

firstname=Joe&lastname=Doe&...

then send this as a POST reqeust. You need to also build a proper set of headers to emulate a real browser sending the request, otherwise the site may refuse to handle it correctly.

You can use Fiddler to first navigate to the site and save the requests in Firefox and then use information from those requests to build your HttpWebRequest objects.

HttpWebRequest works in both synchronous and asynchronous modes, so you can either download a page using a few lines of code or you can control the entire download process.

OTHER TIPS

You can just submit using the javascript:

<script type="text/javascript">
  document.forms["your_form_id"].submit();
</script>

You can put javascript in after the page loads to just submit the form.

document.forms[0].submit();

Assuming you only have one form on the page, otherwise you can put the name of the form in quotes inside the forms[] brackets.

Am I right in saying that the page with the form on it does not belong on your website, and that you are reading some form on an external site?

If you have all the fields, can't you also read the 'action' attribute on the form tag and perform a POST to the same location, with all the keys/values that you have already got?

You can use HttpWebRequest to do this in server-side code, and just send the POST data that way.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top