I need to simulate an HTTP POST programmatically, i.e., I need to generate a Request with some POST variables and then send it to a page.
To clarify, I need to simulate the behaviour of a regular POST, not do the whole thing programmatically. So basically I need to fill in a Request in the same way it would be filled if a form POST was happening, and then send the browser to the page that expects the POST.
I asked this question in the past here:
POST a HTML Form programmatically?
I was directed to the following link which worked great for me!:
WebClient.UploadValues
I don't think it is easy to do what you want, but there may be an acceptable workaround. Here are some ideas for workarounds:
You need HttpWebRequest class.
Here is one way to do it.
You send this method the url and the name/value parameters in the form of a NameValueCollection. The method makes a Http Post on the endpoint and returns the response as a string.
Of course depending on what/why you're doing this and how many times this method will be called, there maybe other alternatives. But until you provide more information on your specific needs, this method is good enough.
The method below uses Tasks (.NET 4.0) and the async methods so it will be faster then a synchronous method like the next code listing if you're making multiple calls in a loop for example.
you could also use WebClient (it's bit simpler). This method expects the post parameters as a string in the form
etc. So if you use this method be sure to pass in your parameters as such or modify the implementation to be like the code above.
Something like this?