There must be an HTTP "HEAD" (and not POST or GET) in order to retrieve only the headers. If you retrieve a code 200 (OK), the file exists. If no, its a 404 (Not found).
The following method returns True if the URL exists (200), False if it does not (404), and throws an exception if another error occurs
Code:
Function CheckUrlExists(url As String) As Boolean
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
request.Method = "HEAD"
Try
Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
If response.StatusCode = HttpStatusCode.OK Then
Return True
Else
Return False
End If
End Using
Catch ex As WebException
Dim response As HttpWebResponse = TryCast(ex.Response, HttpWebResponse)
If response IsNot Nothing AndAlso response.StatusCode = HttpStatusCode.NotFound Then
Return False
Else
Throw
End If
End Try
End Function
Bookmarks