Jeswin
Jeswin

Reputation: 238

Create HTTP post request and receive response in C# application and parsing the response

I am trying to write a C# program which posts http form to a CGI script and obtains a response.

The response is web page which needs to be parsed as I need only a certain portion of it.

Is there any way I can obtain the response web page and parse it in C# ?

Upvotes: 1

Views: 2206

Answers (2)

BrokenGlass
BrokenGlass

Reputation: 160892

You can use a combination of WebClient for sending the form and retrieving the response and HtmlAgilityPack for parsing the result for this task.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

You could use the WebClient class:

using System.Collections.Specialized;
using System.Net;

class Program
{
    static void Main()
    {
        using (var client = new WebClient())
        {
            var values = new NameValueCollection();
            values["foo"] = "bar";
            values["bar"] = "baz";
            var url = "http://foo.bar/baz.cgi";
            byte[] result = client.UploadValues(url, values);

            // TODO: do something with the result
            // for example if it represents text you could
            // convert this byte array into a string using the proper
            // encoding: string sResult = Encoding.UTF8.GetString(result);
        }
    }
}

Upvotes: 0

Related Questions