简体   繁体   中英

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

您可以结合使用WebClient发送表单和检索响应,以及结合使用HtmlAgilityPack来分析此任务的结果。

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM