Asp.net HttpWebResponse - 如何才能不依赖 WebException 进行流量控制?
我需要检查请求是否会返回 500 服务器内部错误(因此预计会出现错误)。我正在这样做:
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
return true;
else
return false;
但是当我收到 500 Internal Server Error 时,会抛出 WebException,并且我不想依赖它来控制应用程序流程 - 如何才能做到这一点?
I need to check whether the request will return a 500 Server Internal Error or not (so getting the error is expected). I'm doing this:
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
return true;
else
return false;
But when I get the 500 Internal Server Error, a WebException is thrown, and I don't want to depend on it to control the application flow - how can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这篇 MSDN 文章会对您有所帮助:
http://msdn.microsoft.com/en- us/library/system.net.webexception.status.aspx
I think this MSDN articles will help you:
http://msdn.microsoft.com/en-us/library/system.net.webexception.status.aspx
事实上,根据 msdn 上的示例,没有办法不依赖控制流的异常。这是他们给出的例子:
显然,有时你确实必须走那条路。啊,好吧。
Indeed, given the example at msdn, there is no way to not depend on the exception for control flow. Here's the example they give:
Apparently, sometimes you do have to go down that route. Ah, well.