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#?
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#?
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;
}
}
MSDN example of HttpWebrequest.GetResponse has working code.
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx
If the question is "How to get the code-behind file of a web page", the answer is no.
If you are planning to do a lot of web requests to access RESTful services, be careful with the HttpWebRequest
object. It takes a while to be reclaimed, and if you have enough traffic (just a few calls a minute), you can start to get strange behavior.
If you are loading other pages dynamically, I'd recommend doing it in JavaScript.