如何在 WP7 中处理 HttpWebRequest 的错误?

发布于 2024-11-16 00:00:55 字数 224 浏览 2 评论 0原文

使用 WebClient 类时,您可以使用以下命令检查错误和空结果

e.error!= null

e.result == null

分别。我将如何使用 HttpWebRequest 类来处理这个问题?所有示例似乎都忽略了这一点,但它在应用中至关重要。

When using the WebClient class you can check for errors and empty results by using

e.error != null

and

e.result == null

respectively. How would I handle this using the HttpWebRequest class? All examples seem to omit this yet its vital in applications.

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

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

发布评论

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

评论(3

潇烟暮雨 2024-11-23 00:00:55

HttpWebRequest 使用 IAsyncResult 和 Begin/End 对进行操作。

您将向 Begin 操作传递一个回调方法委托,然后在该回调中调用该操作的 End 方法。要捕获操作的异步部分中可能发生的错误,请在对 End 方法的调用周围放置一个 try 块。

例如,当调用 BeginGetResponse 时,您可以传递此回调:-

 private void Callback(IAsyncResult asyncResult)
 {
     try 
     {
         HttpWebResponse resp =  (HttpWebResponse)myRequest.EndGetResponse(asyncResult);
     }
     catch (Exception e)
     {
         //Something bad happened during the request
     }

 }

The HttpWebRequest uses the IAsyncResult and Begin/End pairs for an operation.

You will have passed a Callback method delegate to Begin operation and then in that callback you call the End method for that operation. To catch an error that may have occured in the asynchronous part of the operation you place a try block around your call to the End method.

For example when calling BeginGetResponse you might pass this call back:-

 private void Callback(IAsyncResult asyncResult)
 {
     try 
     {
         HttpWebResponse resp =  (HttpWebResponse)myRequest.EndGetResponse(asyncResult);
     }
     catch (Exception e)
     {
         //Something bad happened during the request
     }

 }
行雁书 2024-11-23 00:00:55

尝试使用 REST 客户端框架,例如 Spring.Rest ("Spring.Rest “在 NuGet 上),它将为您完成所有这些样板代码:

RestTemplate client = new RestTemplate("http://exemple.com/");
client.GetForObjectAsync<string>("path/", r =>
  {
    if (r.Error != null)
    {
    }
  });

Try a REST client Framework like Spring.Rest ("Spring.Rest" on NuGet), it will do all this boilerplate code for you :

RestTemplate client = new RestTemplate("http://exemple.com/");
client.GetForObjectAsync<string>("path/", r =>
  {
    if (r.Error != null)
    {
    }
  });
勿忘初心 2024-11-23 00:00:55

您可以使用 try-catch 来实现这一点。

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

You can use try-catch for that.

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

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