本文介紹了我怎樣才能捕捉到 404?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問題描述
我有以下代碼:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
request.Credentials = MyCredentialCache;
try
{
request.GetResponse();
}
catch
{
}
如何捕獲特定的 404 錯(cuò)誤?WebExceptionStatus.ProtocolError 只能檢測(cè)到發(fā)生了錯(cuò)誤,而不能給出錯(cuò)誤的確切代碼.
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;
}
}
只是還不夠有用...協(xié)議異常可能是 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
是一個(gè) WebException
.
這篇關(guān)于我怎樣才能捕捉到 404?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!