Sergio
Sergio

Reputation: 8269

How to post to a Web page

This is something I am curious about since I learnt how to invoke an url and get a http response so I could parse the results in my application. Something like what Chris M says here:

Faking browser request in ASP.net C#

Now what I am wondering is how can I post a form that I download in this way, filling in the fields of the form.

I don't really need this for my work, it's just to kill my curiosity as a programmer :)

Upvotes: 2

Views: 313

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064084

The easiest way (in C#) to simulate a form post with values:

using (WebClient client = new WebClient())
{
    NameValueCollection fields = new NameValueCollection();
    fields.Add("foo", "123");
    fields.Add("bar", "abc");
    client.UploadValues(address, fields);
}

Just for completeness, jQuery can do it more efficiently again...

$.post(address, { foo: "123", bar: "abc" } );

If you want to inspect the html to create your POST, either use WebBrowser and automation, or use the Html Agility Pack to look at it.

Upvotes: 4

Related Questions