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