我怎样才能抓到404?
我有以下代码:
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
就像是:
HttpWebResponse errorResponse = we.Response as HttpWebResponse; if (errorResponse.StatusCode == HttpStatusCode.NotFound) { // }
哪里
we
是一个WebException
。
try { var request = WebRequest.Create(uri); using (var response = request.GetResponse()) { using (var responseStream = response.GetResponseStream()) { // Process the stream } } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) { var resp = (HttpWebResponse) ex.Response; if (resp.StatusCode == HttpStatusCode.NotFound) { // Do something } else { // Do something else } } else { // Do something else } }
我没有testing过这个,但是应该可以
try { // TODO: Make request. } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse resp = ex.Response as HttpWebResponse; if (resp != null && resp.StatusCode == HttpStatusCode.NotFound) { // TODO: Handle 404 error. } else throw; } else throw; }
在C#6中,您可以使用exceptionfilter 。
try { var request = WebRequest.Create(uri); using (var response = request.GetResponse()) using (var responseStream = response.GetResponseStream()) { // Process the stream } } catch(WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { // handle 404 exceptions } catch (WebException ex) { // handle other web exceptions }
我想如果你发现了一个WebException,那里有一些信息可以用来判断它是否是一个404.这是我现在唯一知道的方法…我有兴趣了解其他的…
catch(WebException e) { if(e.Status == WebExceptionStatus.ProtocolError) { var statusCode = (HttpWebResponse)e.Response).StatusCode); var description = (HttpWebResponse)e.Response).StatusDescription); } }
看看这个snipit。 GetResponse会抛出一个WebRequestException。 赶上,你可以从响应中获得状态码。
try { // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name. HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("invalid site"); // Get the associated response for the above request. HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse(); myHttpWebResponse.Close(); } catch(WebException e) { Console.WriteLine("This program is expected to throw WebException on successful run."+ "\n\nException Message :" + e.Message); if(e.Status == WebExceptionStatus.ProtocolError) { Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode); Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription); } } catch(Exception e) { Console.WriteLine(e.Message); }
这来自http://msdn.microsoft.com/en-us/library/system.net.webexception.status.aspx
StatusCode属性是我认为,你正在寻找。
在MSDN上查看响应的状态:
... catch(WebException e) { Console.WriteLine("The following error occured : {0}",e.Status); } ...
对于浏览这个VB.NET的人来说,我相信只有它真的是404,我们才能捕捉到这个exception。例如:
Try httpWebrequest.GetResponse() Catch we As WebException When we.Response IsNot Nothing _ AndAlso TypeOf we.Response Is HttpWebResponse _ AndAlso (DirectCast(we.Response, HttpWebResponse).StatusCode = HttpStatusCode.NotFound) ' ... End Try
捕获适当的exceptiontypesWebException
:
try { var request = (HttpWebRequest) WebRequest.Create(String.Format("http://www.gravatar.com/avatar/{0}?d=404", hashe)); using(var response = (HttpWebResponse)request.GetResponse()) Response.Write("has avatar"); } catch(WebException e) { if(e.Response.StatusCode == 404) Response.Write("No avatar"); }
当使用WebRequest类的POST或GET数据到服务器时,那么exception的types将是WebException.Below是文件未findexception的代码
//Create a web request with the specified URL string path = @"http://localhost/test.xml1"; WebRequest myWebRequest = WebRequest.Create(path); //Senda a web request and wait for response. try { WebResponse objwebResponse = myWebRequest.GetResponse(); Stream stream= objwebResponse.GetResponseStream(); } catch (WebException ex) { if (((HttpWebResponse)(ex.Response)).StatusCode == HttpStatusCode.NotFound) { throw new FileNotFoundException(ex.Message); } }