簡體   English   中英

創建HTTP發布請求並在C#應用程序中接收響應並解析響應

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

我正在嘗試編寫一個將http表單發布到CGI腳本並獲得響應的C#程序。

響應是網頁,需要解析,因為我只需要解析其中的一部分。

有什么方法可以獲取響應網頁並將其解析為C#嗎?

您可以結合使用WebClient發送表單和檢索響應,以及結合使用HtmlAgilityPack來分析此任務的結果。

您可以使用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