接住一个特定的WebException(550) [英] Catching a specific WebException (550)

查看:237
本文介绍了接住一个特定的WebException(550)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我创建和执行System.Net.FtpWebRequest。

Let's say I create and execute a System.Net.FtpWebRequest.

我可以使用捕获(WebException前){}捕捉任何网络相关的异常抛出这一要求,但如果我有一些逻辑,我只希望在异常因抛给执行(550)未找到文件

I can use catch (WebException ex) {} to catch any web related exception thrown by this request, but what if I have some logic that I only want to execute when the exception is thrown due to (550) file not found.

什么是做到这一点的最好方法是什么?

What's the best way to do this?

我可以复制的异常信息,并检验相等的:

I can copy the exception message and test for equality:

 const string fileNotFoundExceptionMessage = "The remote server returned an error: (550) File unavailable (e.g., file not found, no access).";
                if (ex.Message == fileNotFoundExceptionMessage) {

但理论上它似乎是这条消息可能会改变的道路。

but theoretically it seems like this message could change down the road.

我只是测试,看是否异常消息包含550,这可能是更容易的工作,如果该消息被改变(它可能仍含有550某处的文字),但当然也会返回true,如果文本其他一些WebException恰好含有550。

I can just test to see if the exception message contains "550", which is probably more likely to work if the message is changed (it will likely still contain 550 somewhere in the text) but of course will also return true if the text for some other WebException just happens to contain 550.

似乎有没有成为用于访问例外的只是数量的方法。

There doesn't seem to be a method for accessing just the number of the exception.

有什么想法?

推荐答案

WebException 暴露了一个<一个href="http://msdn.microsoft.com/en-us/library/system.net.webexception.status.aspx"><$c$c>Status$c$c属性,你可以检查。

WebException exposes a StatusCode property that you can check.

如果你想实际的HTTP响应code,你可以做这样的事情:

If you want the actual HTTP response code you can do something like this:

(int)((HttpWebResponse)ex.Response).StatusCode

这篇关于接住一个特定的WebException(550)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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