![](/img/trans.png)
[英]Uploading a blob using HttpWebRequest in silverlight 5 causes 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.