检测404而不捕获异常 [英] Detect 404 without catching exceptions

查看:263
本文介绍了检测404而不捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单功能:检查Web服务器是否返回非200 HTTP状态。

 私有功能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)
返回(rsp.StatusCode = HttpStatusCode.OK)
结束功能

我从这个答案如何检查文件是否在网络服务器上退出网址是什么?



不幸的是,它不起作用:抛出一个System.Net.WebException,当远程服务器返回一个错误:(404)Not Found,当url指向到不存在的页面。我想能够使用HEAD请求(或类似的东西)来探测服务器,然后处理404,而不必捕获异常。



我的修复看起来像这是:

 私有函数RemoteFileOk(ByVal Url As String)As Boolean 
Dim req As HttpWebRequest = TryCast(WebRequest。创建(Url),HttpWebRequest)
req.Method =HEAD
尝试
使用rsp As HttpWebResponse = TryCast(req.GetResponse(),HttpWebResponse)
返回(rsp。 StatusCode = HttpStatusCode.OK)
结束使用
抓取ex作为WebException
返回False
结束尝试
结束函数
/ pre>

但是,当我们从不喜欢使用try-catch语句时,似乎可以避免 。



是否有另一个更整洁的方式?

解决方案

您可以更新代码以使用新的系统。 Net.Http API在.net 4.5中不会抛出这个令人烦恼的异常(见questi的评论)。测试在VS 2012u2下工作。

 私有功能RemoteFileOk(ByVal Url As String)As Boolean 
使用客户端作为新HttpClient,
responseTask As Task(Of HttpResponseMessage)= client.GetAsync(Url,HttpCompletionOption.ResponseHeadersRead)
responseTask.Wait()
使用响应作为HttpResponseMessage = responseTask.Result
返回response.IsSuccessStatusCode
结束使用
结束使用
结束功能

当问题被提出时,这个答案是不可用的,对许多人来说,这不是一个解决方案,因为这个API是在.net 4.5中引入的。


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?

解决方案

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.

这篇关于检测404而不捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆