Download HTML-source with vb.net

Paste in a module and just call the function like this:
HelloString = GetHTML(“http://www.cnn.com/”)


Private requestweb As HttpWebRequest
Private responseWeb As HttpWebResponse

Public Function GetHTML(ByVal URL)
Dim WebSource As String = ""
Dim objStreamReader As StreamReader = Nothing

requestweb = CType(WebRequest.Create(URL), HttpWebRequest)
With requestweb
.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36"
.Method = "GET"
.Timeout = 10000
End With

Try
responseWeb = CType(requestweb.GetResponse(), HttpWebResponse)
Catch ex As Exception
MessageBox.Show("Error retrieving the Web page " & _
"you requested. Please check the entered Url and your internet connection")
Exit Function
End Try

If Not IsNothing(responseWeb.GetResponseStream()) Then
Try
'objStreamReader = New StreamReader(responseWeb.GetResponseStream())
objStreamReader = New StreamReader(responseWeb.GetResponseStream(), Encoding.UTF8)
WebSource = objStreamReader.ReadToEnd
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Function
Finally
responseWeb.Close()
objStreamReader.Close()
End Try

End If
Return (WebSource)
End Function

Leave a Reply

Your email address will not be published. Required fields are marked *