1

我试图从异步调用写入表单。我在网上看到的示例表明这应该可以工作,但我一直收到错误消息。

首先,尽管 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";
    }
}
4

1 回答 1

1

从我所见,您的请求/响应代码本质上没有任何问题。由于 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 应用程序,使用代理”。基本上,使用您的“友好”服务器设置 WCF 或 ASMX Web 服务调用,并使用 clientaccesspolicy.xml 文件,使用您的服务器发出 http 请求,然后返回结果。我不确定谷歌是否支持 JSONP,但你也可以试试。

于 2009-01-29T20:22:55.903 回答