簡體   English   中英

Silverlight 4中使用HttpWebRequest跨域的SecurityException

[英]SecurityException from using HttpWebRequest cross-domain in Silverlight 4

我正在嘗試在Silverlight應用程序中編寫一個函數,該函數請求與托管Silverlight應用程序所在的域不在同一個域中的特定頁面。

例如:

但是,這會生成“ SecurityException”:

{System.Security.SecurityException:安全錯誤。 在System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult)...}

據我了解,這與跨域請求受到限制有關,並發現了一些提及該文章的帖子(http://msdn.microsoft.com/zh-cn/library/cc197955(VS.95).aspx )可能與之相關。

這是我的代碼:

public static void CheckPageContentsAsync(CheckPageContentsCallback callback, DependencyObject uiObject)
{
    bool result = false;
    try
    {
        HttpWebRequest request = WebRequest.CreateHttp("http://www.mysite.com/MyPage.aspx");
        request.BeginGetResponse((asyncHandle) =>
        {
            try
            {
                uiObject.Dispatcher.BeginInvoke(new VoidDelegate(() =>
                {

                    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncHandle);
                    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                    {
                        string content = sr.ReadToEnd();
                        if (content.Contains("Value"))
                        {
                            result = true;
                        }

                        if (callback != null)
                        {
                            callback.Invoke(result);
                        }
                    }
                }), null);
            }
            catch (Exception excep)
            {
                throw new Exception("Failed to process response.", excep);
            }
        }, null);
    }
    catch(Exception excep2)
    {
        throw new Exception("Failed to generate request.", excep2);
    }
}

尚未完全了解“ clientaccesspolicy.xml”或“ crossdomain.xml”文件作為解決方案的適用性。

誰能清楚地解釋我如何修改我的應用程序或我所請求的Web服務器以解決此問題?

我曾經將此文件復制到我的應用程序的根目錄中:

<cross-domain-policy>
    <allow-access-from domain="*.*" headers="SOAPAction"/>
    <allow-http-request-headers-from domain="*.*" headers="SOAPAction"/> 
    <site-control permitted-cross-domain-policies="master-only"/>
</cross-domain-policy>

將其命名為crossdomain.xml。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM