views:

36

answers:

0

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?