Home > Backend Development > C++ > How Can I Retrieve HTML Source Code Using C#?

How Can I Retrieve HTML Source Code Using C#?

Patricia Arquette
Release: 2025-01-05 00:50:39
Original
883 people have browsed it

How Can I Retrieve HTML Source Code Using C#?

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");
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template