Get Error number in WebException Error

前端 未结 4 1801
再見小時候
再見小時候 2021-02-19 04:40

How To Get Error number in WebException Error?

try
{
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(\"site\");
     HttpWebResponse response         


        
相关标签:
4条回答
  • 2021-02-19 05:05

    For Get Error Number:

    catch(System.Net.WebException e)
    {
        int errorNumber = (int)e.Status;
    }
    
    0 讨论(0)
  • 2021-02-19 05:12

    You could try to parse the message, but there isn't always an error number. A timeout for example doesn't result in an HTTP error code.

    0 讨论(0)
  • 2021-02-19 05:12

    You can't do that because WebException(s) don't have error numbers. You can define your own error numbers if that's what you want/need to do.

    Here's the documentation You can get Status, Message, StackTrace, etc, etc.

    0 讨论(0)
  • 2021-02-19 05:14

    You'll want to run a test to make sure that it was a ProtocolError:

    if (e.Status == WebExceptionStatus.ProtocolError) 
    {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
    }
    
    0 讨论(0)
提交回复
热议问题