Get Error number in WebException Error

眉间皱痕 提交于 2019-12-21 07:29:33

问题


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?
}

回答1:


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);
}



回答2:


For Get Error Number:

catch(System.Net.WebException e)
{
    int errorNumber = (int)e.Status;
}



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/7440922/get-error-number-in-webexception-error

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