Silverlight WebClient 未收到 400 错误请求

发布于 2024-12-01 11:42:41 字数 313 浏览 1 评论 0原文

我有一个 WebClient,并且正在订阅 OnDownloadStringCompleted 事件处理程序。当服务器响应 400 Bad Request 标头时,永远不会触发 OnDownloadStringCompleted。即使标题显示 400,我仍然需要响应是什么。有没有办法绕过这个?

这是我尝试获取的 URL: https://graph.facebook.com/me?access_token=your_token

I have a WebClient and I'm subscribing to the OnDownloadStringCompleted event handler. When the server responds with a header of 400 Bad Request, the OnDownloadStringCompleted Never gets triggered. I still need what the response is even though the header says 400. Is there a way to bypass this?

Heres the URL I'm attempting to fetch:
https://graph.facebook.com/me?access_token=your_token

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

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

发布评论

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

评论(1

药祭#氼 2024-12-08 11:42:41

首先,在我的测试中,我发现 DownloadStringCompleted 确实会触发。

但是,尝试读取事件参数 Result 属性将引发错误。您需要测试事件参数 Error 属性以确定是否发生错误。如果它具有此属性,则将保存对 WebException 的引用。

幸运的是,WebException 有一个Response 对象,其类型为WebResponse。您可以使用一个小函数从中获取响应字符串:-

    string StringFromWebResponse(WebResponse response)
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            return reader.ReadToEnd();
        }
    }

但有一个问题。仅当使用 ClientHTTP 堆栈而不是 BrowserHTTP 堆栈时才可用。所以你的应用程序中需要这样的代码:-

    WebRequest.RegisterPrefix("https://graph.facebook.com", System.Net.Browser.WebRequestCreator.ClientHttp);

那么这样的代码就可以工作:-

        WebClient client = new WebClient();
        client.DownloadStringCompleted += (s, args) =>
        {
            string result;

            if (args.Error == null)
            {
                result = args.Result;
                //Do something with the expected result
            }
            else
            {
                WebException err = args.Error as WebException;

                result = StringFromWebResponse(err.Response);
                // Do something with the error result
            }
        };

        client.DownloadStringAsync(new Uri("https://graph.facebook.com/me?access_token=your_token", UriKind.Absolute));

哦,但可能还有另一个问题。我对 facebook API 一无所知,但如果存在对 cookie 的依赖,那么默认情况下 ClientHTTP 堆栈本身不管理 cookie。要正确处理 cookie,您需要为每个使用的 HttpWebRequest 分配一个通用的 CookieContainerWebClient 不允许您访问所使用的 WebRequest 对象,因此您需要转而使用 WebRequest/WebResponse 直接。

First off all in my testing I find that the DownloadStringCompleted does fire.

However an attempt to read the event args Result property will throw an error. You need to test the event args Error property to determine if an error occured. If it has this property will hold a reference to a WebException.

Fortunately the WebException has a Response object which is of type WebResponse. You can get the response string from this using a little function:-

    string StringFromWebResponse(WebResponse response)
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            return reader.ReadToEnd();
        }
    }

But there is a problem. This is only available when using the ClientHTTP stack not the BrowserHTTP stack. So you need something like this line of code in your app:-

    WebRequest.RegisterPrefix("https://graph.facebook.com", System.Net.Browser.WebRequestCreator.ClientHttp);

So then code like this will work:-

        WebClient client = new WebClient();
        client.DownloadStringCompleted += (s, args) =>
        {
            string result;

            if (args.Error == null)
            {
                result = args.Result;
                //Do something with the expected result
            }
            else
            {
                WebException err = args.Error as WebException;

                result = StringFromWebResponse(err.Response);
                // Do something with the error result
            }
        };

        client.DownloadStringAsync(new Uri("https://graph.facebook.com/me?access_token=your_token", UriKind.Absolute));

Oh but there is possibly another problem. I don't know anything about the facebook API but if there is a dependency on cookies then by default the ClientHTTP stack doesn't manage cookies itself. To handle cookies properly you need to assign a common CookieContainer to each HttpWebRequest used. WebClient doesn't allow you access to the WebRequest object used so you'll need to drop to using WebRequest/WebResponse directly.

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