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

How can I get the HTML of a page in C # ASP.NET?

Example: http://google.com

How can I get this HTML code using ASP.NET C #?

+3
source share
4 answers

WebClient the 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 may prefer to use the asynchronous version DownloadStringto 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;
    }
}
+17
source

Example MSDN HttpWebrequest.GetResponse has a working code.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx

+1
source

: " -", .

0

If you plan on making many web requests to access RESTful services, be careful with the object HttpWebRequest. It takes some time to fix, and if you have enough traffic (just a few minutes per minute), you can start strange behavior.

If you load other pages dynamically, I would recommend doing this in JavaScript.

-1
source

Source: https://habr.com/ru/post/1724305/


All Articles