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?

Community
  • 1
  • 1
Jakob Gade
  • 12,319
  • 15
  • 70
  • 118
  • 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? – Robert Harvey Oct 28 '10 at 05:02
  • Exactly. Just about to type that. – RPM1984 Oct 28 '10 at 05:04
  • 2
    This answer says this is one of those "vexing exceptions":http://stackoverflow.com/questions/1366848/httpwebrequest-getresponse-throws-webexception-on-http-304 – Ani Oct 28 '10 at 05: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. – Jakob Gade Oct 28 '10 at 05:15
  • @Ani: Thanks for the link, that’s a nice article (http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx) 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. – Jakob Gade Oct 28 '10 at 05:20

1 Answers1

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.

binki
  • 7,754
  • 5
  • 64
  • 110