本文介紹了我怎樣才能捕捉到 404?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有以下代碼:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
request.Credentials = MyCredentialCache;
try
{
request.GetResponse();
}
catch
{
}
如何捕獲特定的 404 錯誤?WebExceptionStatus.ProtocolError 只能檢測到發生了錯誤,而不能給出錯誤的確切代碼.
How can I catch a specific 404 error? The WebExceptionStatus.ProtocolError can only detect that an error occurred, but not give the exact code of the error.
例如:
catch (WebException ex)
{
if (ex.Status != WebExceptionStatus.ProtocolError)
{
throw ex;
}
}
只是還不夠有用...協議異??赡苁?401、503、403 等等.
Is just not useful enough... the protocol exception could be 401, 503, 403, anything really.
推薦答案
使用HttpStatusCode Enumeration
,特別是HttpStatusCode.NotFound
類似于:
HttpWebResponse errorResponse = we.Response as HttpWebResponse;
if (errorResponse.StatusCode == HttpStatusCode.NotFound) {
//
}
在哪里we
是一個 WebException
.
這篇關于我怎樣才能捕捉到 404?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!