我有以下代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "HEAD"; request.Credentials = MyCredentialCache; try { request.GetResponse(); } catch { }
如何捕获特定的404错误?WebExceptionStatus.ProtocolError只能检测到发生错误,而不能给出错误的确切代码。
例如:
catch (WebException ex) { if (ex.Status != WebExceptionStatus.ProtocolError) { throw ex; } }
只是不够有用…协议异常可能是401、503、403,实际上是任何东西。
HttpStatusCode Enumeration专门使用HttpStatusCode.NotFound
HttpStatusCode Enumeration
HttpStatusCode.NotFound
就像是:
HttpWebResponse errorResponse = we.Response as HttpWebResponse; if (errorResponse.StatusCode == HttpStatusCode.NotFound) { // }
哪里 we是WebException。
we
WebException