Catching a specific WebException (550)

别来无恙 提交于 2019-12-04 05:09:59

WebException exposes a StatusCode property that you can check.

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

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

For reference, here's the actual code I ended up using:

catch (WebException ex) {
    if (ex.Status == WebExceptionStatus.ProtocolError &&
        ((FtpWebResponse)ex.Response).StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) {
        // Handle file not found here
    }

Declare a WebException object, casting the ex value from your Catch block to it. Then you can check the StatusCode Property.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!