Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Comparing three methods
Message
De
20/11/2011 16:10:32
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Comparing three methods
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01529322
Message ID:
01529322
Vues:
84
I have those three methods which does pratically the same thing. They pull out the content from a Web page:
        ' Get the HTML response from a server
        Public Function GetXML() As Boolean
            Dim llSuccess As Boolean = False
            Dim loRequest As System.Net.WebRequest = Nothing
            Dim loResponse As System.Net.WebResponse = Nothing
            Dim loStream As Stream = Nothing
            Dim loStreamReader As StreamReader = Nothing

            ' Reset the values
            cMessage = ""
            cXML = ""

            Try

                ' Get the URL
                loRequest = System.Net.WebRequest.Create(cUrl)
                loResponse = loRequest.GetResponse()
                loStream = loResponse.GetResponseStream()
                loStreamReader = New StreamReader(loStream)
                cXML = loStreamReader.ReadToEnd()

                llSuccess = True
            Catch loError As Exception
                cMessage = loError.Message
            End Try

            Return llSuccess
        End Function

        ' Get the HTML response from a server
        ' expC1 Url
        Public Function GetUrl(ByVal tcUrl As String) As String
            Dim lcHTML As String = ""
            Dim loHTTP As System.Net.WebClient = New System.Net.WebClient
            Dim loEncoding As System.Text.Encoding = New System.Text.UTF8Encoding
            Dim loRequestedHTML() As Byte

            Try
                loRequestedHTML = loHTTP.DownloadData(tcUrl)
                lcHTML = loEncoding.GetString(loRequestedHTML)
            Catch loError As Exception
                lcHTML = loError.Message
            End Try

            Return lcHTML
        End Function

        ' Get the HTML response from a server
        ' expC1 Url
        ' expN1 Timeout
        Public Function GetUrlTimeout(ByVal tcUrl As String, Optional ByVal tnTimeout As Integer = 5000) As String
            Dim lcHTML As String = ""
            Dim lcUrl As New System.Uri(tcUrl)
            Dim loHTTP As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(lcUrl)

            loHTTP.Timeout = tnTimeout

            Try
                Dim loWebResponse As System.Net.WebResponse = loHTTP.GetResponse()
                Dim loStream As System.IO.Stream = loWebResponse.GetResponseStream()
                Dim loString As New System.IO.StreamReader(loStream)
                lcHTML = loString.ReadToEnd()
            Catch loError As Exception
                lcHTML = loError.Message
            End Try

            Return lcHTML
        End Function
The first one uses the System.Net.WebRequest, the second one uses System.Net.WebClient and the last one uses System.Net.HttpWebRequest so it can benefit of the timeout feature as well as System.Net.NetworkCredential if I would need to authenticate againsts a Web page. I am looking at a universal method to do it all. I assume the last one would probably be the best to use. Is that correct?

In all of them, no matter if the URL returns an XML or a HTML response, I believe the last method would be able to handle it all.
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform