0

I am having small problem geeting HttpStatusCode from response. Problem is when file exists I am getting response and can read read status, but when file doesn't exists I don't see any status, even if I asked to show me status string. Here is my code:

 Dim urls As New List(Of String)
        urls.Add("http://www.domain.com/test.php")
        urls.Add("http://www.domain.com/test2.php")
        urls.Add("http://www.domain.com/index.php")


        For Each Url As String In urls
            Dim response As HttpWebResponse = Nothing
            Try
                Dim request As HttpWebRequest = Net.HttpWebRequest.Create(Url)
                request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
                request.Method = "GET"
                response = request.GetResponse()

            Catch webex As WebException
            End Try

            If response.StatusCode = HttpStatusCode.OK = True Then
                MsgBox("File Url is correct: " & response.StatusCode.ToString)
            ElseIf response.StatusCode = HttpStatusCode.NotFound = True Then
                MsgBox("File Url is incorrect: " & Url)
            Else
                MsgBox(response.StatusCode.ToString)
            End If
        Next
DGibbs
  • 14,316
  • 7
  • 44
  • 83
Chelovek
  • 159
  • 2
  • 3
  • 12
  • possible duplicate of [How to properly catch a 404 error in .NET](http://stackoverflow.com/questions/2149208/how-to-properly-catch-a-404-error-in-net) – CodeCaster Jul 11 '13 at 13:29

3 Answers3

2

The problem is that when the file does not exist, it generates a WebException, and your code is silently "swallowing" those exceptions. ie. it catches it and does nothing.

You need to add some code that checks for the error inside your catch statement.

This may be a duplicate of How to properly catch a 404 error in .NET (although C# rather than VB)

Community
  • 1
  • 1
tonks
  • 73
  • 5
2

When the server does not return a success status code (2xx), the framework always throws an exception. However you can still get the response from the exception object.

Function GetResponse(url As Uri) As WebResponse
    Dim response As WebResponse
    Dim request As HttpWebRequest = HttpWebRequest.Create(url)
    Try
        response = request.GetResponse()
    Catch serverErrors As WebException When serverErrors.Response IsNot Nothing
        response = serverErrors.Response
    Catch otherExceptions As Exception
        DoSomethingWith(otherExceptions)
    End Try
    Return response
End Function
Steven Liekens
  • 13,266
  • 8
  • 59
  • 85
0

Correct me if I'm wrong but shouldn't this part:

ElseIf response.StatusCode = HttpStatusCode.NotFound = True Then
           MsgBox("File Url is incorrect: " & Url)

Actually be:

ElseIf response.StatusCode = HttpStatusCode.NotFound = True Then
           MsgBox("File Url is incorrect: " & Url & response.StatusCode.ToString)

If you want the StatusCode to be shown.

Regarding your comment:

but when file doesn't exists I don't see any status

The code is hitting the NotFound enum and entering the code block but you aren't acutally showing the status in your code.

DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • I wanted to see string which file doesn`t exist first when status is HttpStatusCode.NotFound. If it would give me another status, why it doesn't show me in `Else MsgBox(response.StatusCode.ToString)` ? – Chelovek Jul 11 '13 at 11:49
  • `" why it doesn't show me in Else MsgBox(response.StatusCode.ToString) "` - Because the code is entering the `ElseIf`. Execution wont continue to the `Else`. If you want them all to be evaluated individually then you need to use `If` statements for all of them. – DGibbs Jul 11 '13 at 11:54
  • But if I change `ElseIf response.StatusCode = HttpStatusCode.NotFound = True Then MsgBox("File Url is incorrect: " & Url)` to `ElseIf response.StatusCode = HttpStatusCode.OK = False Then MsgBox("File Url is incorrect: " & Url)` I don`t see it too. – Chelovek Jul 11 '13 at 12:02
  • I see now. You need to read up on execution flow. If you have a bunch of else ifs, the code will only enter the first one it encounters. It will then break out of the if/elseif block and continue on. If you want each to be evaluated you need to change them to all be `If` statements. – DGibbs Jul 11 '13 at 12:06
  • Ok, fixed, replaced ` Catch webex As WebException End Try` with ` Catch webex As WebException 'MsgBox(webex.Message.ToString) Continue For End Try` It keeps For function running further. – Chelovek Jul 11 '13 at 12:22