4

Simple function: Check if a webserver returns a non-200 HTTP status.

Private Function RemoteFileOk(ByVal Url As String) As Boolean
  Dim req As HttpWebRequest = TryCast(WebRequest.Create(Url), HttpWebRequest)
  req.Method = "HEAD"
  Dim rsp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
  Return (rsp.StatusCode = HttpStatusCode.OK)
End Function

I got it from this answer on "How to check if a file exits on an webserver by its URL?".

Unfortunately, it doesn't work: A System.Net.WebException is thrown, “The remote server returned an error: (404) Not Found” when the url points to a non-existent page. I would like to be able to probe the server with a HEAD request (or something similar) and then deal with the 404 without having to catch exceptions.

My fix looks like this:

Private Function RemoteFileOk(ByVal Url As String) As Boolean
  Dim req As HttpWebRequest = TryCast(WebRequest.Create(Url), HttpWebRequest)
  req.Method = "HEAD"
  Try
    Using rsp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
      Return (rsp.StatusCode = HttpStatusCode.OK)
    End Using
  Catch ex As WebException
    Return False
  End Try
End Function

But I never liked using try-catch statements when it seems they could be avoided.

Is there another, neater, way?

5
  • 3
    Seems like an exception is appropriate for a 404, since there is no way for the web server to "handle" it, other than tossing an error page. Why do you consider an exception bad here? Commented Oct 28, 2010 at 5:02
  • Exactly. Just about to type that. Commented Oct 28, 2010 at 5:04
  • 2
    This answer says this is one of those "vexing exceptions":stackoverflow.com/questions/1366848/… Commented Oct 28, 2010 at 5:04
  • 3
    @Robert and @RPM1984: I don’t think a missing file on a remote server is so extraordinary that it should result in an exception in my code. :) An exception (and catching it) carries a small performance penalty and it clutters the code unnecessarily. So I’m basically just curios if there’s something hidden somewhere in the .NET framework that can solve this common task without resorting to exception handling. Commented Oct 28, 2010 at 5:15
  • @Ani: Thanks for the link, that’s a nice article (blogs.msdn.com/b/ericlippert/archive/2008/09/10/…) and I share Eric Lipperts points of view. And I actually think 404’s somehow fall in the “boneheaded” category, although the contents on the remote server are out of my hands. Commented Oct 28, 2010 at 5:20

1 Answer 1

0

You can update your code to use the new System.Net.Http API in .net 4.5 which doesn’t throw this vexing exception (see question’s comments). Tested to work under VS 2012u2.

Private Function RemoteFileOk(ByVal Url As String) As Boolean
    Using client As New HttpClient,
        responseTask As Task(Of HttpResponseMessage) = client.GetAsync(Url, HttpCompletionOption.ResponseHeadersRead)
        responseTask.Wait()
        Using response As HttpResponseMessage = responseTask.Result
            Return response.IsSuccessStatusCode
        End Using
    End Using
End Function

This answer was not available when the question was asked and, for many people, is not a solution because this API was introduced in .net 4.5.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.