简体   繁体   English

Silverlight 4中使用HttpWebRequest跨域的SecurityException

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

I'm trying to write a function in my Silverlight app that requests a particular page that doesn't exist on the same domain as where my Silverlight app is hosted. 我正在尝试在Silverlight应用程序中编写一个函数,该函数请求与托管Silverlight应用程序所在的域不在同一个域中的特定页面。

For example: 例如:

However, this generates a 'SecurityException': 但是,这会生成“ SecurityException”:

{System.Security.SecurityException: Security error. {System.Security.SecurityException:安全错误。 at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) ...} 在System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult)...}

From what I understand, this is related to cross-domain requests being restricted, and found some posts that mentioned that this article (http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx) might be related. 据我了解,这与跨域请求受到限制有关,并发现了一些提及该文章的帖子(http://msdn.microsoft.com/zh-cn/library/cc197955(VS.95).aspx )可能与之相关。

Here's my code: 这是我的代码:

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);
    }
}

Haven't been able to make much sense of the applicability of the "clientaccesspolicy.xml" or "crossdomain.xml" files as a solution. 尚未完全了解“ clientaccesspolicy.xml”或“ crossdomain.xml”文件作为解决方案的适用性。

Can anyone explain clearly how I modify my app, or the web server I'm requesting from, to resolve this issue? 谁能清楚地解释我如何修改我的应用程序或我所请求的Web服务器以解决此问题?

I use to copy this file in the root of my app: 我曾经将此文件复制到我的应用程序的根目录中:

<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>

Name it crossdomain.xml. 将其命名为crossdomain.xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM