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

标签 c# .net silverlight windows-phone-7 httpwebrequest

使用 WebClient 类时,您可以使用

检查错误和空结果

e.error != null

e.result == null

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

最佳答案

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

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

例如,当调用 BeginGetResponse 时,您可能会将此回调传回:-

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

 }

关于c# - 如何在 WP7 中处理 HttpWebRequest 的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6379860/

相关文章:

c# - System.IO.FileNotFoundException : Could not load file or assembly 'Silvernium, 版本=1.0.4254.29979

silverlight - WP7 PathGeometry 错误

c# - C# 中的超小型 Web 服务器

c# - XNA 游戏分辨率高于 PC 分辨率 - 它会导致问题吗?

c# - 从下拉列表中获取选定的值 asp.net

.net - 使用 xbuild 在 ubuntu 上使用 SandcaSTLe HelpFileBuilder 生成 .NET 文档

c# - Application 和 AppDomain.CurrentDomain 有什么区别

c# - 在 C# Tcp 服务器中调试可能孤立的套接字

.net - 这个匿名类型具体类型是什么?

c# - 如何在 WP7 中使用 Dispatcher 进行异步调用?