为什么 WP7 模拟器没有捕获 C# .NET WebException?

发布于 2024-11-12 05:27:48 字数 1142 浏览 3 评论 0原文

我正在开发一个 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 技术交流群。

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

发布评论

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

评论(1

晨与橙与城 2024-11-19 05:27:48

研究告诉我,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

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