我有以下代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
request.Credentials = MyCredentialCache;
try
{
request.GetResponse();
}
catch
{
}
Run Code Online (Sandbox Code Playgroud)
如何捕获特定的404错误?WebExceptionStatus.ProtocolError只能检测到发生了错误,但未提供错误的确切代码.
例如:
catch (WebException ex)
{
if (ex.Status != WebExceptionStatus.ProtocolError)
{
throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
只是没用得多......协议异常可能是401,503,403,真的是什么.
.net c# error-handling exception-handling http-status-code-404