Retrieving HTML Source with C#
When working with HTML documents, it is often useful to be able to access the raw HTML source. This can be achieved in C# using the WebClient class from the System.Net namespace. The WebClient class encapsulates a variety of methods for downloading and interacting with web resources.
Using WebClient.DownloadFile()
To download the complete HTML source for a given URL, you can use the DownloadFile() method. The following code sample demonstrates this technique:
using System.Net; using (WebClient client = new WebClient()) // WebClient class inherits IDisposable { client.DownloadFile("http://yoursite.com/page.html", @"C:\localfile.html"); // Or you can get the file content without saving it string htmlCode = client.DownloadString("http://yoursite.com/page.html"); }
The DownloadFile() method takes two arguments: the URL of the HTML file and the local path where the file is to be saved. Alternatively, you can pass null as the second argument to retrieve the content of the file without saving it. In this case, the DownloadString() method returns the contents of the HTML file as a string.
The above is the detailed content of How Can I Retrieve HTML Source Code Using C#?. For more information, please follow other related articles on the PHP Chinese website!