简体   繁体   English

创建HTTP发布请求并在C#应用程序中接收响应并解析响应

[英]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. 我正在尝试编写一个将http表单发布到CGI脚本并获得响应的C#程序。

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# ? 有什么方法可以获取响应网页并将其解析为C#吗?

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

You could use the WebClient class: 您可以使用WebClient类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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