开发者

Get Error number in WebException Error

How To Get Error number in WebException Error?

try
{
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("site");
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     Stream stream = respons开发者_JAVA技巧e.GetResponseStream();
     int i = stream.ReadByte();
}
catch (WebException e)
{
     //How To Get Error number in WebException Error?
}


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


For Get Error Number:

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜