We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

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


Related Tags:

ASP.NET, HTML

Author: Naibedya Kar

top

ASP.NET

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login