Wednesday, April 13, 2011

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:

http://stackoverflow.com/questions/642860/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 :)

From stackoverflow
  • 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.

    Sergio : WebBrowser and automation I will not consider because when i will try to "play" without any user interface. client.UploadValues looks like the easy way to do what i have in mind :) thanks

0 comments:

Post a Comment