2011-09-16 36 views
12

Cách nhận số lỗi trong Lỗi ngoại lệ trên web?Nhận số lỗi trong Lỗi ngoại lệ trên web

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

Trả lời

24

Bạn sẽ muốn chạy một kiểm tra để đảm bảo rằng đó là một 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

Làm thế nào có được ** HTTP Substatus ** giá trị? Ví dụ: _404.13 Độ dài nội dung quá lớn_ Tham khảo: http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits – Kiquenet

0

Bạn không thể làm điều đó vì WebException(s) không có số lỗi. Bạn có thể xác định số lỗi của riêng bạn nếu đó là những gì bạn muốn/cần phải làm.

Here's the documentation Bạn có thể nhận Status, tin nhắn, StackTrace, vv, vv

0

Bạn có thể thử để phân tích thông điệp, nhưng không phải lúc nào cũng là một số lỗi. Ví dụ: thời gian hết giờ không dẫn đến mã lỗi HTTP.

6

Đối Nhận Error Number:

catch(System.Net.WebException e) 
{ 
    int errorNumber = (int)e.Status; 
} 
Các vấn đề liên quan