DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Check Web Connection

  • submit to reddit
        // Checks access to a web connection by looking for a file on the web server.
// Changing the text in that file allows manually turning off merge synchronization remotely.

    Public Function CheckWebConnection() As Boolean
        Dim URL As String = ""
        Dim rString As String = ""
        'Address of URL'
        If My.Settings.Online = False Then
            Return False
        End If

            'Get HTML data'
            Dim request As HttpWebRequest = WebRequest.Create(URL)
            request.Credentials = New NetworkCredential("WebUSerName", "WebUserPass")
            If My.Settings.HasProxy Then
                Dim ProxyCreds As New NetworkCredential(My.Settings.ProxyUsername, My.Settings.ProxyPassword)
                Dim prxy As New WebProxy(My.Settings.ProxyAddress & ":" & My.Settings.ProxyPort)
                prxy.Credentials = ProxyCreds
                'Get HTML data'
                request.Proxy = prxy
            End If

            Dim response As HttpWebResponse = request.GetResponse()
            Dim reader As StreamReader = New StreamReader(response.GetResponseStream())

            Dim str As String = reader.ReadLine()
            rString = rString & str
            If rString = "Success" Then
                Return True
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function