Get Error number in WebException Error
Asked Answered
R

3

17

How To Get Error number in WebException Error?

try
{
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("site");
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     Stream stream = response.GetResponseStream();
     int i = stream.ReadByte();
}
catch (WebException e)
{
     //How To Get Error number in WebException Error?
}
Rendition answered 16/9, 2011 at 6:25 Comment(0)
I
30

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);
}
Isothermal answered 16/9, 2011 at 6:29 Comment(1)
How get HTTP Substatus value ? For example, 404.13 Content Length Too Large Reference: iis.net/configreference/system.webserver/security/…Manzanares
R
7

For Get Error Number:

catch(System.Net.WebException e)
{
    int errorNumber = (int)e.Status;
}
Rendition answered 21/9, 2011 at 10:27 Comment(0)
I
0

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.

Indoxyl answered 16/9, 2011 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.