Handling two WebException's properly

后端 未结 1 577
野趣味
野趣味 2020-12-15 19:05

I am trying to handle two different WebException\'s properly.

Basically they are handled after calling WebClient.DownloadFile(string address, str

相关标签:
1条回答
  • 2020-12-15 19:50

    Based on this MSDN article, you could do something along the following lines:

    try
    {
        // try to download file here
    }
    catch (WebException ex)
    {
        if (ex.Status == WebExceptionStatus.ProtocolError)
        {
            if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
            {
                // handle the 404 here
            }
        }
        else if (ex.Status == WebExceptionStatus.NameResolutionFailure)
        {
            // handle name resolution failure
        }
    }
    

    I'm not certain that WebExceptionStatus.NameResolutionFailure is the error you are seeing, but you can examine the exception that is thrown and determine what the WebExceptionStatus for that error is.

    0 讨论(0)
提交回复
热议问题