How to get the webpage source in ASP.NET C#?

asp.net, c#, httpwebrequest, methods

Solution

The `WebClient` class will do what you want:

string address = "http://stackoverflow.com/";   

using (WebClient wc = new WebClient())
{
    string content = wc.DownloadString(address);
}

As mentioned in the comments, you might prefer to use the async version of `DownloadString` to avoid blocking:

string address = "http://stackoverflow.com/";

using (WebClient wc = new WebClient())
{
    wc.DownloadStringCompleted +=
        new DownloadStringCompletedEventHandler(DownloadCompleted);
    wc.DownloadStringAsync(new Uri(address));
}

// ...

void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if ((e.Error == null) && !e.Cancelled)
    {
        string content = e.Result;
    }
}

Problem

How can I get the HTML code of a page in C# ASP.NET? Example: `http://google.com` How I can get this HTML code by ASP.NET C#?

Original source