尝试在 Silverlight 2 中使用 HttpWebRequest 时出错

发布于 2024-07-12 08:20:46 字数 1201 浏览 5 评论 0原文

我试图从异步调用写入表单。 我在网上看到的例子表明这应该有效,但我不断收到错误。

首先,尽管 Disbatcher 从未调用过 p,但仍进行了调用。 其次,我在调用 req.EndGetResponse(a); 时收到 System.Security.SecurityException;

可能是什么原因导致了这个问题?

public partial class Page : UserControl
{
    Uri url = new Uri("http://www.google.com", UriKind.Absolute);
    HttpWebRequest req;

    private delegate void PrintToUIThread(string text);
    PrintToUIThread p;

    public Page()
    {
        InitializeComponent();
        req = (HttpWebRequest)WebRequest.Create(url);
        p = new PrintToUIThread(print);
        req.BeginGetResponse(new AsyncCallback(WebComplete), req);
    }

    void WebComplete(IAsyncResult a)
    {
        try
        {
            Dispatcher.BeginInvoke(p);
            HttpWebRequest req = (HttpWebRequest)a.AsyncState;
            HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(a);
            Dispatcher.BeginInvoke(p);
        }
        catch (Exception ex)
        {
            print(ex.ToString());
        }
    }

    private void print(string text)
    {
        PageTextBox.Text = text;
    }

    private void print()
    {
        PageTextBox.Text = "Call From Invoke";
    }
}

Im trying to write to a form from an asynchronous call. The examples I seen on line show that this should work but I keep getting an error.

First the call that is made though the Disbatcher never calls p. Second i get a System.Security.SecurityException on the call req.EndGetResponse(a);

What could be causing the problem?

public partial class Page : UserControl
{
    Uri url = new Uri("http://www.google.com", UriKind.Absolute);
    HttpWebRequest req;

    private delegate void PrintToUIThread(string text);
    PrintToUIThread p;

    public Page()
    {
        InitializeComponent();
        req = (HttpWebRequest)WebRequest.Create(url);
        p = new PrintToUIThread(print);
        req.BeginGetResponse(new AsyncCallback(WebComplete), req);
    }

    void WebComplete(IAsyncResult a)
    {
        try
        {
            Dispatcher.BeginInvoke(p);
            HttpWebRequest req = (HttpWebRequest)a.AsyncState;
            HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(a);
            Dispatcher.BeginInvoke(p);
        }
        catch (Exception ex)
        {
            print(ex.ToString());
        }
    }

    private void print(string text)
    {
        PageTextBox.Text = text;
    }

    private void print()
    {
        PageTextBox.Text = "Call From Invoke";
    }
}

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

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

发布评论

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

评论(1

层林尽染 2024-07-19 08:20:46

据我所知,您的请求/响应代码没有本质上的错误。 由于 google 的 crossdomain.xml 文件隐含的跨域限制,您可能会遇到安全异常。

这是 Google 的 crossdomain.xml:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <site-control permitted-cross-domain-policies="by-content-type" />
</cross-domain-policy>

这一切实际上是在说“嘿,RIA 应用程序,使用代理”。 基本上,使用带有 clientaccesspolicy.xml 文件的“友好”服务器设置 WCF 或 ASMX Web 服务调用,使用您的服务器发出 http 请求,然后将结果传回。 我不确定 google 是否支持 JSONP,但你也可以尝试一下。

There's nothing inherently wrong with your request/response code from what I can see. You're probably running into the security exception because of the cross-domain restrictions implied by google's crossdomain.xml file.

This is Google's crossdomain.xml:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <site-control permitted-cross-domain-policies="by-content-type" />
</cross-domain-policy>

All this is really saying is "Hey, RIA app, use a proxy". Basically, set up a WCF or ASMX web service call with your "friendly" server with a clientaccesspolicy.xml file in place, use your server to make the http request, then deliver the results back. I'm not not sure if google supports JSONP, but you could try that as well.

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