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("http://www.asp.net");
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string html = sr.ReadToEnd();

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

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

Hope that helps.

2 Comments

Comments have been disabled for this content.