1

我正在使用以下方法从网站获取响应流 -

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

2 回答 2

1

如果您需要在 Silverlight 中从不同的网站/域提供内容,请查看以下指南:

http://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx

我希望这有帮助。

于 2012-10-26T16:04:17.517 回答
0

我通过创建一个 Web 方法解决了这个问题,该方法与前端代码在后端 Web 服务中所做的完全一样,然后在前端调用它,我已经通过之前添加这些文件实现了跨域访问策略。

于 2012-11-05T13:47:04.827 回答