在 Silverlight 4 中使用 HttpWebRequest 跨域时出现 SecurityException
我正在尝试在我的 Silverlight 应用程序中编写一个函数,该函数请求一个特定页面,该页面与我的 Silverlight 应用程序所在的域不存在。
例如:
- Silverlight 应用程序:http://www.mysilverlightsite.com/
- 目标页面:http://www.mysite.com/MyPage.aspx
但是,这会生成“SecurityException”:
{System.Security.SecurityException: 安全错误。在 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult 异步结果)在 System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult 异步结果)...}
据我了解,这与跨域请求被限制有关,并发现一些帖子提到了本文(http://msdn.microsoft.com/en-us/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”文件作为解决方案的适用性。
谁能清楚地解释我如何修改我的应用程序或我请求的网络服务器来解决此问题?
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.
For example:
- Silverlight App: http://www.mysilverlightsite.com/
- Target Page: http://www.mysite.com/MyPage.aspx
However, this generates a 'SecurityException':
{System.Security.SecurityException:
Security error. at
System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult
asyncResult) at
System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult
asyncResult) ...}
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.
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.
Can anyone explain clearly how I modify my app, or the web server I'm requesting from, to resolve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常将此文件复制到应用程序的根目录中:
将其命名为 crossdomain.xml。
I use to copy this file in the root of my app:
Name it crossdomain.xml.