HttpWebRequest 异常处理

发布于 2024-10-20 23:31:02 字数 870 浏览 1 评论 0原文

我正在创建一个异步 HttpWebRequest,如果失败,我想调用备份 Web 服务。像这样:

public void CallService1()
{
    HttpWebRequest request = HttpWebRequest.Create("http://MyFirstWebService")
    request.BeginGetResponse(this.CallService1Completed, request);
}

public void CallService1Completed(IAsyncResult result)
{
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;

    try
    {
        using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
        {
            using (Stream responseStream = response.GetResponseStream())
            {   
                // Use Data 
            }
        }
    }
    catch (WebException webException)
    {
        if (?????)
        {
            CallBackupService2();
        }
    }
}

请记住,这是一个移动应用程序,您可能并不总是有互联网连接。如果没有互联网连接,我不想调用备份服务。我只想在第一个服务因某种原因关闭时调用备份服务。我会在上面的“IF”语句中添加什么。

I am making an asyncronous HttpWebRequest and if that fails, I want to call a backup web service. Like so:

public void CallService1()
{
    HttpWebRequest request = HttpWebRequest.Create("http://MyFirstWebService")
    request.BeginGetResponse(this.CallService1Completed, request);
}

public void CallService1Completed(IAsyncResult result)
{
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;

    try
    {
        using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
        {
            using (Stream responseStream = response.GetResponseStream())
            {   
                // Use Data 
            }
        }
    }
    catch (WebException webException)
    {
        if (?????)
        {
            CallBackupService2();
        }
    }
}

Bearing in mind that this is a mobile applications where you may not always have an internet connection. I do not want to call the backup service if there is no internet connection. I only want to call the backup service if the first service is down for some reason. What would I put in the 'IF' statement above.

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

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

发布评论

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

评论(1

原来分手还会想你 2024-10-27 23:31:02

它可以这样实现:

if (NetworkInterface.GetIsNetworkAvailable())
{
   CallBackupService2();
}

It can be implemented like:

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