How to Read the HTML of a Web Page Programmatically

We might need to read the contents of some page (local or remote) by code. This is quite simple in .net.

using System.Net;
using System.IO;

WebRequest req = WebRequest.Create("");
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string html = sr.ReadToEnd();

The string html will then hold the html contents of We can also use relative uris in the same website:

WebRequest req = WebRequest.Create(new Uri("somepage.aspx", UriKind.Relative));

Hope that helps.


Comments have been disabled for this content.