我使用以下方法从网站获取响应流 -
HttpWebRequest request;
WebResponse response;
private void button1_Click(object sender, RoutedEventArgs e)
{
string url = textBox1.Text;
request = WebRequest.Create(url) as HttpWebRequest;
request.BeginGetResponse(AfterRequest, null);
}
public void AfterRequest(IAsyncResult result)
{
response = request.EndGetResponse(result);
using (StreamReader sd = new StreamReader(response.GetResponseStream()))
{
string resultString = sd.ReadToEnd();
}
response.Close();
}
然而,代码在到达时会中断 -
response = request.EndGetResponse(result);
出现错误信息 -
SecurityException was unhandled by user code
导致此问题的原因是什么?
听到完全例外 -
System.Security.SecurityException ---> System.Security.SecurityException: Security error.
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa. <EndGetResponse>b__9(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)
--- End of inner exception stack trace ---
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
答案 0 :(得分:1)
如果您需要在Silverlight中从其他网站/域提供内容,请查看以下指南:
http://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx
我希望这会有所帮助。
答案 1 :(得分:0)
我通过创建一个Web方法解决了这个问题,该方法完全按照前端代码在后端Web服务中执行的操作,然后在前端调用它,我已经通过添加这些文件来实现跨域访问策略先前。