使用 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/