How to read a remote web page with ASP.NET 2.0

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.


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();
  return strResult;
Shared Function GetHtmlPage(ByVal strURL As String) As String

  Dim strResult As String
  Dim objResponse As WebResponse
  Dim objRequest As WebRequest = HttpWebRequest.Create(strURL)
  objResponse = objRequest.GetResponse()
  Using sr As New StreamReader(objResponse.GetResponseStream())
    strResult = sr.ReadToEnd()
  End Using
  Return strResult
End Function

And to call the method:

string TheUrl = "";
string response = GetHtmlPage(TheUrl);
Dim TheUrl As String = ""
Dim response As String = GetHtmlPage(TheUrl)

The HttpWebRequest object can be configured to supply header information. Most of the default values are null. For example, the User-Agent value is null, but can be set to anything you want. Cookies can also be "enabled" for the object through the CookiesContainer property, if required to maintain sessions.

Date Posted:
Last Updated:
Posted by:
Total Views to date: 104401


- titus

How to get the content with out tag,images and unwanted information from this return result.Kindly help me

- Mike


You need to use Regualr Expressions. Google "Regular Expressions Strip HTML" for examples.

- Michael

It seems there is a loss in translation with the reader.
The original file that is read has : 7S£ §I‚xsß*ُ£
The information read
from the file only reads : 7S�� �I�xs�*ُ�

Any ideas? or are the characters just not allowed

- Michael

Hello again. Thanks all for the help.
I just converted to binary.

- kumar

Hi mike
Is there any technique through which we can read a single line or value from a webpage like reading a specific number from a webpage

Pawan Kumar

- Dipesh

Simple & Awesme... tq...

- mahendra

thanx a lot

- Tina

When I try the C# version, I just get a blank response. It's as if it's not loading the URL at all. No errors, just nothing returned. Help?

Recent Comments

Justin Kusuma 8/4/2015 7:01 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Hi Mike, thanks for replying... I'm looking for more elegant way to provide user feedback. As now...

Arthur 8/3/2015 4:54 PM
In response to Getting the identity of the most recently added record
Thanks Mike, your site is a mine of useful info :)...

MRP 8/3/2015 4:06 AM
In response to Entity Framework Recipe: Many To Many Relationship On The Same Table
my question is about HashSet and ICollection ! why you use this type and why not user Ilist<> and ?...

Arbaz 8/1/2015 3:39 PM
In response to Scheduled Tasks In ASP.NET With Quartz.Net
Hey Mike, What If I want to set a schedule weekly or every Sunday to run. What would the code for I...

Emma 7/31/2015 3:29 AM
In response to How To Send Email In ASP.NET MVC
As mentioned by John Noble, there seems to be something missing in regards to: return...

Satyabrata Mohapatra 7/30/2015 6:42 AM
In response to Implementing Google's EU End User Consent Policy
Thanks.... Learned a lot....

Jaswanth 7/29/2015 1:31 PM
In response to Displaying Google Analytics Data in ASP.NET
Hi Mike, My application uses MVC 1. I need to fetch the data of pages which has top views. I tried...

Sergey 7/28/2015 6:48 AM
In response to Custom TagHelpers in ASP.NET MVC 6
Thanks a lot!...

Dome 7/27/2015 7:55 PM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Hi, I've just created a new project with WebApi & MVC, then created a controller with the following...

Ted Driver 7/26/2015 5:44 PM
In response to How To Send Email In ASP.NET MVC
Thanks for the great example Mike! I noticed in my implementation of your code that if I put some in...