为什么 WP7 模拟器没有捕获 C# .NET WebException?
我正在开发一个 Windows Phone 7 应用程序,我需要将一些数据放在我们公司的 REST api 上。
我正在测试这个 API 调用,并遇到了我无法捕获的 WebException。我的调用最初是在 lambda 中进行的,因此我将其提取到成员函数中,但仍然没有成功。
错误字符串是“远程服务器返回错误:NotFound”。这并不意外,我的应用程序尝试的 URI 可能无效,因为我搞砸了,或者因为我们的临时服务器已关闭。
如果我无法捕获此异常,我该如何检查我的请求是否失败?
这是我的玩具示例:
void TriggerApiCall()
{
var request = HttpWebRequest.Create(new Uri(
"http://api.ourcompany.com" +
"/stuff/" +
someParameterizedPath + "?" +
"firstParam=" + someClassMember +
"&secondParam=8badf00d"));
request.Method = "PUT";
request.BeginGetResponse(TryCall, request);
}
private void TryCall(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
var completed = result.IsCompleted;
WebResponse response = null;
try {
bool hasresp = request.HaveResponse;
response = request.EndGetResponse(result);
} catch (Exception e) {
System.Diagnostics.Debug.WriteLine(e); // Never reaches here,
// Exception propagates to top
}
response.Close();
}
I'm developing a Windows Phone 7 app, and I need to PUT some data on our company's REST api.
I was testing out this API call, and ran into a WebException that I couldn't catch. My call was originally in a lambda, so I pulled it out into member functions, but still no luck.
The error string is "The remote server returned an error: NotFound". Which isn't unexpected, a URI that my app tries might not be valid either because I screwed up, or because our staging server is down.
How the heck am I supposed to check if my request failed if I can't catch this exception?
Here's my toy example:
void TriggerApiCall()
{
var request = HttpWebRequest.Create(new Uri(
"http://api.ourcompany.com" +
"/stuff/" +
someParameterizedPath + "?" +
"firstParam=" + someClassMember +
"&secondParam=8badf00d"));
request.Method = "PUT";
request.BeginGetResponse(TryCall, request);
}
private void TryCall(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
var completed = result.IsCompleted;
WebResponse response = null;
try {
bool hasresp = request.HaveResponse;
response = request.EndGetResponse(result);
} catch (Exception e) {
System.Diagnostics.Debug.WriteLine(e); // Never reaches here,
// Exception propagates to top
}
response.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
研究告诉我,Silverlight 3 及更高版本中有一个新的 ClientHttp 堆栈。
http://msdn.microsoft.com/en-我们/library/dd920295(v=vs.95).aspx
Research tells me there's a new ClientHttp stack in Silverlight 3 and above.
http://msdn.microsoft.com/en-us/library/dd920295(v=vs.95).aspx