« Using VB.Net HTTPWebRequest, HTTPWebResponse, WebClient to Get a Web Page »
Thu July-19-2007, 8:12:18 PM
The following code shows three different ways, using different .Net objects to get the source of a web page. These objects are HTTPWebRequest, HTTPWebResponse, WebRequest, WebResponse, and WebClient.

You can download the complete project from the download link below.

Source code of the project is also included.

    'Using HttpWebRequest and HttpWebResponse
    Public Function getHTMLSource1(ByVal url As String) As String
        Dim HTMLSource As String = ""
        Dim Request As HttpWebRequest = WebRequest.Create(url)
        Dim Response As HttpWebResponse = Request.GetResponse
        Dim SR As StreamReader
        SR = New StreamReader(Response.GetResponseStream)
        HTMLSource = SR.ReadToEnd
        SR.Close()
        Return HTMLSource
    End Function

    'Using Webclient
    Public Function getHTMLSource2(ByVal url As String) As String
        Dim HTMLSource As String = ""
        Dim WC As New WebClient
        Dim SR As StreamReader
        SR = New StreamReader(WC.OpenRead(url))
        HTMLSource = SR.ReadToEnd
        SR.Close()
        Return HTMLSource
    End Function

    'Using WebRequest WebResponse
    Public Function getHTMLSource3(ByVal url As String) As String
        Dim HTMLSource As String = ""
        Dim Request As WebRequest = WebRequest.Create(url)
        Dim Response As WebResponse = Request.GetResponse
        Dim SR As StreamReader
        SR = New StreamReader(Response.GetResponseStream)
        HTMLSource = SR.ReadToEnd
        SR.Close()
        Return HTMLSource
    End Function

    'Using Webclient With Credentials
    Public Function getHTMLSource4(ByVal url As String) As String
        Dim HTMLSource As String = ""
        Dim WC As New WebClient
        WC.Credentials = New NetworkCredential("USERNAME", "PASSWORD")
        Dim SR As StreamReader
        SR = New StreamReader(WC.OpenRead(url))
        HTMLSource = SR.ReadToEnd
        SR.Close()
        Return HTMLSource
    End Function

 

 
12th
May
2009
Gregor
at 7:08am
Hi,

Do you have any idea, why I get error: (401) Unauthorized, when I use your code. Should I somehow include username and password.

Could you help me, please.

Regards, Gregor
Darong on May 12th 2009 6:41pm replied:
Hey, Gregor

I have updated the code with basic http authentications.

You can download the updated source in the zip file.

Basically, you just need to add the following line.

WC.Credentials = New NetworkCredential("USERNAME", "PASSWORD")

Hope this helps.

Darong
wayne on Jun 25th 2009 3:17pm replied:
Your code works for me but now I need to display the output in a AxWebBrowser. And when that page is navigated to I get challenged again. Do I need to cache something?

regards
wayne