How can you easily check the http error number of a webclient?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alex21
    New Member
    • Oct 2008
    • 19

    How can you easily check the http error number of a webclient?

    I'm trying to detect the http status number such as (401 Unauthorized) from a 'WebException' when a WebClient in my code fails.

    Code:
    Public Function DataSources_ValidURL() As Boolean
            Dim TestClient As New WebClient()
            TestClient.BaseAddress = DataSources_FilePath
            Try
                TestClient.DownloadString(DataSources_FilePath)
            Catch ex As WebException
                If ex.Status = 401 Then
                    MessageBox.Show("Testing")
                End If
                If ex.Response.Headers.AllKeys.Contains("WWW-Authenticate") Then
                    If MessageBox.Show("The remote server is requesting a username and password.", "Password Required", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
    
                    Else
    
                    End If
                End If
    
                MessageBox.Show(String.Format("An error was encountered whilst trying to connect to the entered web address: {0}", ex.Message), "Error Encountered")
                Return False
            End Try
    
            Return True
        End Function
    I would rather check for the error number then for an authentication header...

    Code:
     If ex.Response.Headers.AllKeys.Contains("WWW-Authenticate") Then
    Does anyone know how to simply check for a 401 status return from a WebException.

    Thanks
Working...