Visual Basic Code Snippet - Download HTML Web Page

Visual Basic Code Snippet - Download HTML Web Page

Visual Basic Code Snippet - Download HTML Web Page

(VB) Visual Basic code snippet download web page HTML source contents.

Bookmark:

Visual Basic Code Snippet - Download HTML Web Page

This .Net Visual Basic code snippet download web page HTML source contents. To use this function simply provide the URL of the web page you like to download. This function read the web page contents and returns HTML source code as a string.

''' <summary>
''' Function to download HTML web page
''' </summary>
''' <param name="_URL">URL address to download web page</param>
''' <returns>HTML contents as a string</returns>
Public Function DownloadHTMLPage(ByVal _URL As String) As String
    Dim _PageContent As String = Nothing
    Try
        ' Open a connection
        Dim _HttpWebRequest As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(_URL), System.Net.HttpWebRequest)

        ' You can also specify additional header values like the user agent or the referer: (Optional)
        _HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
        _HttpWebRequest.Referer = "http://www.google.com/"

        ' set timeout for 10 seconds (Optional)
        _HttpWebRequest.Timeout = 10000

        ' Request response:
        Dim _WebResponse As System.Net.WebResponse = _HttpWebRequest.GetResponse()

        ' Open data stream:
        Dim _WebStream As System.IO.Stream = _WebResponse.GetResponseStream()

        ' Create reader object:
        Dim _StreamReader As New System.IO.StreamReader(_WebStream)

        ' Read the entire stream content:
        _PageContent = _StreamReader.ReadToEnd()

        ' Cleanup
        _StreamReader.Close()
        _WebStream.Close()
        _WebResponse.Close()
    Catch _Exception As Exception
        ' Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString())
        Return Nothing
    End Try

    Return _PageContent
End Function


Here is a simple example showing how to use above function (DownloadHTMLPage) to download web page HTML contents and show it in a RichTextBox.

Dim _WebContents As String = Nothing
' Read HTML contents from a web page
_WebContents = DownloadHTMLPage("http://www.digitalcoding.com/")

' Show HTML contents in RichTextBox
If _WebContents IsNot Nothing Then
    richTextBox1.Text = _WebContents
End If


VB Keywords Used:

  • HttpWebRequest
  • WebResponse
  • Stream
  • StreamReader
  • ExecuteReader
  • Exception

Code Snippet Information:

  • Applies To: .Net, VB, Visual Basic, CLI, HTML, HttpWebRequest, WebResponse, StreamReader, Data Mining
  • Programming Language : Visual Basic (VB)

External Resources:

John Cellar :: August 26-2009 :: 06:07 PM

I am looking for a VB function in Excel VB6 (not VB.net) to get/read a web page to obtain a string of HTM as above in VB.net.

Any help would be greatly appreciated.

John Cellar

Leave a comment