<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 在 C# 中讀取網頁 > 原文: [https://zetcode.com/csharp/readwebpage/](https://zetcode.com/csharp/readwebpage/) 在本文中,我們展示了如何在 C# 中抓取網頁。 [C# 教程](http://zetcode.com/lang/csharp/)是有關 C# 語言的綜合教程。 本教程顯示了如何使用`HttpWebRequest`,`WebClient`,`HttpClient`,`Flurl.Http`和`RestSharp`讀取頁面。 在本教程的示例中,我們從一個小型網頁 [webcode.me](http://www.webcode.me) 中讀取了一個網頁。 ## C# 使用`HttpClient`讀取網頁 `HttpClient`提供了一個基類,用于從 URI 標識的資源發送 HTTP 請求和接收 HTTP 響應。 `Program.cs` ```cs using System; using System.Net.Http; using System.Threading.Tasks; namespace DownloadPageHttpClient { class Program { static async Task Main(string[] args) { using var client = new HttpClient(); client.DefaultRequestHeaders.Add("User-Agent", "C# console program"); var content = await client.GetStringAsync("http://webcode.me"); Console.WriteLine(content); } } } ``` 該代碼示例使用`HttpClient`異步抓取網頁。 ```cs var content = await client.GetStringAsync("http://webcode.me"); ``` `await`運算符將`awaitable`作為參數; 檢查是否已經完成等待; 如果等待已完成,則該方法繼續運行。 `GetStringAsync()`將內容讀取為字符串,作為異步操作。 ```cs $ dotnet run <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My html page</title> </head> <body> <p> Today is a beautiful day. We go swimming and fishing. </p> <p> Hello there. How are you? </p> </body> </html> ``` 這是輸出。 ## 用`WebClient`讀取網頁 `WebClient`提供了用于向 URI 標識的資源發送數據和從中接收數據的通用方法。 `Program.cs` ```cs using System; using System.Net; namespace DownloadPageWebClient { class Program { static void Main(string[] args) { using var client = new WebClient(); client.Headers.Add("User-Agent", "C# console program"); string url = "http://webcode.me"; string content = client.DownloadString(url); Console.WriteLine(content); } } } ``` 該代碼示例使用`WebClient`獲取網頁。 ```cs string content = client.DownloadString(url); ``` `DownloadString()`方法檢索指定的資源。 此方法在下載資源時阻塞。 在第二個示例中,我們為`WebClient`提供了一種非阻塞方法。 `Program.cs` ```cs using System; using System.Net; using System.Threading.Tasks; namespace DownloadPageWebClientAsync { class Program { static void Main(string[] args) { using var client = new WebClient(); client.DownloadStringCompleted += (sender, e) => { Console.WriteLine(e.Result); }; string url = "http://www.webcode.me"; client.DownloadStringAsync(new Uri(url)); Console.ReadLine(); } } } ``` 該代碼示例使用`WebClient`獲取網頁的 HTML 代碼。 這次操作是異步的。 ```cs client.DownloadStringCompleted += (sender, e) => { Console.WriteLine(e.Result); }; ``` `DownloadStringCompleted`事件在異步資源下載操作完成時發生。 ```cs client.DownloadStringAsync(new Uri(url)); ``` `DownloadStringAsync`方法下載指定為`String`或`Uri`的資源。 該方法不會阻塞調用線程。 ## C# 使用`HttpWebRequest`讀取網頁 `HttpWebRequest`類提供對屬性和方法的支持,這些屬性和方法使用戶可以使用 HTTP 直接與服務器進行交互。 此 API 現在已標記為過時。 `Program.cs` ```cs using System; using System.Net; using System.IO; namespace DownloadPageHttpWebRequest { class Program { static void Main(string[] args) { string html = string.Empty; string url = "http://webcode.me"; HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url); request.UserAgent = "C# console client"; using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { html = reader.ReadToEnd(); } Console.WriteLine(html); } } } ``` 該示例讀取站點的內容并將其打印到控制臺中。 ```cs HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url); ``` 用`WebRequest.Create()`方法創建一個`HttpWebRequest`。 它以 URL 作為參數。 ```cs using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) ``` 從請求中,我們使用`GetResponse()`方法獲得了`HttpWebResponse`。 ```cs using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { html = reader.ReadToEnd(); } ``` 我們將網頁的內容讀入字符串。 ```cs Console.WriteLine(html); ``` 數據被打印到控制臺。 ## C# 使用`Flurl.Http`讀取網頁 Flurl.Http 是用于 C# 語言的流暢,可移植,可測試的 HTTP 第三方客戶端庫。 ```cs $ dotnet add package Flurl.Http ``` 我們安裝`Flurl.Http`包。 `DownloadPageFlurl.cs` ```cs using System; using System.Threading.Tasks; using Flurl.Http; namespace DownloadPageFlurl { class Program { static async Task Main(string[] args) { string result = await "http://webcode.me".GetStringAsync(); Console.WriteLine(result); } } } ``` 該示例讀取一個小型網頁并將其內容打印到終端。 ```cs string result = await "http://webcode.me".GetStringAsync(); ``` `await`運算符應用于異步方法中的任務,以暫停該方法的執行,直到等待的任務完成為止。 該任務代表正在進行的工作。 使用`GetStringAsync()`擴展方法檢索數據。 ## 用`RestSharp`讀取網頁 RestSharp 是.NET 的簡單 REST 和 HTTP API 客戶端。 它是一個第三方庫。 ```cs $ dotnet add package RestSharp ``` 我們安裝`RestSharp`包。 `Program.cs` ```cs using System; using RestSharp; namespace DownloadPageRestSharp { class Program { static void Main(string[] args) { var client = new RestClient("http://webcode.me"); var request = new RestRequest("", Method.GET); client.ExecuteAsync(request, response => { Console.WriteLine(response.Content); }); Console.ReadLine(); } } } ``` 該代碼示例使用 RestSharp 庫獲取網頁的內容。 該網頁是異步下載的。 ```cs var client = new RestClient("http://www.something.com"); ``` 使用`RestClient`類創建一個其他客戶端。 ```cs var request = new RestRequest("", Method.GET); ``` 使用`RestRequest`創建 GET 請求。 ```cs client.ExecuteAsync(request, response => { Console.WriteLine(response.Content); }); ``` 該請求使用`ExecuteAsync()`方法異步執行。 在本文中,我們展示了如何使用 C# 讀取網頁。 您可能也對以下相關教程感興趣: [MySQL C# 教程](/db/mysqlcsharptutorial/), [C# 中的日期和時間](/articles/csharpdatetime/),[用 C# 讀取文本文件](/articles/csharpreadtext/)或 [C# Winforms 教程](/gui/csharpwinforms/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看