Monday, March 21, 2011

How to read a remote web page in Asp.Net

Two classes in the System.Net namespace make it very easy to obtain the html of a remote web page. These are the HttpWebRequest and HttpWebResponse. Here's a quick demo.

The static method below makes an http request for a web page, and the resulting string of html within the http response is captured and returned to the caller.


//using System.Net;
//using System.IO;
static string GetHtmlPage(string strURL)
{

String strResult;
WebResponse objResponse;
WebRequest objRequest = HttpWebRequest.Create(strURL);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
sr.Close();
}
return strResult;
}


And to call the method:
string TheUrl = "http://www.something.com/Default.aspx";
string response = GetHtmlPage(TheUrl);



OR

WebClient webclient = new WebClient();
string source = webclient.DownloadString("http://www.something.com/Default.aspx");

1 comment:

Ajax CalendarExtender displaying at wrong position in Chrome

< script type ="text/javascript" language ="javascript">     function onCalendarShown(sender, args)...