I'm trying to detect the http status number such as (401 Unauthorized) from a 'WebException' when a WebClient in my code fails.
I would rather check for the error number then for an authentication header...
Does anyone know how to simply check for a 401 status return from a WebException.
Thanks
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
Code:
If ex.Response.Headers.AllKeys.Contains("WWW-Authenticate") Then
Thanks