Sometimes we may need to get the HTML contents of a web page programmatically and without loading it in a browser. The following code can be used for doing it. Let’s take a look..
Private Function ReturnWebData(ByVal URL As String) As String Dim WebResltStr As String = Nothing Dim DtStrm As Stream = Nothing Dim StrmRdr As StreamReader = Nothing Dim WebReqst As WebRequest = Nothing Dim WebResp As HttpWebResponse = Nothing Try 'Create the web request WebReqst = WebRequest.Create(URL) WebReqst.Credentials = CredentialCache.DefaultCredentials 'Get the response WebResp = DirectCast(WebReqst.GetResponse(), HttpWebResponse) If WebResp.StatusDescription = "OK" Then Try DtStrm = WebResp.GetResponseStream() StrmRdr = New StreamReader(DtStrm) WebResltStr = StrmRdr.ReadToEnd() Finally StrmRdr.Close() DtStrm.Close() End Try End If WebResp.Close() Return (WebResltStr) Catch ex As Exception Return (Nothing) End Try End Function