How to read a remote web page with ASP.NET 2.0
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.
[C#]
//System.Net
//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;
}
[VB]
'System.Net
'System.IO
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()
sr.Close()
End Using
Return strResult
End Function
And to call the method:
[C#] string TheUrl = "http://www.mikesdotnetting.com/Default.aspx"; string response = GetHtmlPage(TheUrl);
[VB] Dim TheUrl As String = "http://www.mikesdotnetting.com/Default.aspx" 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.
Currently rated 5.00 by 6 people
Rate Now!
Date Posted:
21 May 2007 21:38
Last Updated:
29 June 2007 08:32
Posted by:
Mikesdotnetting
Total Views to date:
9588
Printer Friendly Version
Comments
09 June 2009 15:59 from Mikesdotnetting
@Titus
You need to use Regualr Expressions. Google "Regular Expressions Strip HTML" for examples.
22 June 2009 20:35 from 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
02 August 2009 23:41 from Michael
Hello again. Thanks all for the help.
I just converted to binary.
28 August 2009 07:15 from 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
thanks
Pawan Kumar



09 June 2009 12:32 from titus
How to get the content with out tag,images and unwanted information from this return result.Kindly help me