Asp.net HttpWebResponse - 如何才能不依赖 WebException 进行流量控制?

发布于 2024-09-04 11:41:51 字数 420 浏览 12 评论 0原文

我需要检查请求是否会返回 500 服务器内部错误(因此预计会出现错误)。我正在这样做:

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
request.Method = "GET";  
HttpWebResponse response = request.GetResponse() as HttpWebResponse; 

if (response.StatusCode == HttpStatusCode.OK)
     return true;
else 
    return false;

但是当我收到 500 Internal Server Error 时,会抛出 WebException,并且我不想依赖它来控制应用程序流程 - 如何才能做到这一点?

I need to check whether the request will return a 500 Server Internal Error or not (so getting the error is expected). I'm doing this:

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
request.Method = "GET";  
HttpWebResponse response = request.GetResponse() as HttpWebResponse; 

if (response.StatusCode == HttpStatusCode.OK)
     return true;
else 
    return false;

But when I get the 500 Internal Server Error, a WebException is thrown, and I don't want to depend on it to control the application flow - how can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你丑哭了我 2024-09-11 11:41:51

事实上,根据 msdn 上的示例,没有办法不依赖控制流的异常。这是他们给出的例子:

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);
}

显然,有时你确实必须走那条路。啊,好吧。

Indeed, given the example at msdn, there is no way to not depend on the exception for control flow. Here's the example they give:

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);
}

Apparently, sometimes you do have to go down that route. Ah, well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文