在Silverlight中获取网页源代码

时间:2013-01-27 11:36:15

标签: c# silverlight

我正在尝试从Silverlight的网页获取源代码。

这是我的代码

        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(WebClient_DownloadStringCompleted);
        wc.DownloadStringAsync(new Uri("http://ip-whois-lookup.com/lookup.php?ip=19.118.245.124"));


    void WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            Mytext.Text = e.Result.ToString();
        }
        else
            Mytext.Text = e.Error.ToString();
    }

这是我的错误。

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__0(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)

同一个url的相同原则适用于c#console aplication

 string htmlContent = new System.Net.WebClient().DownloadString("http://ip-whois-lookup.com/lookup.php?ip=19.118.245.124");

我正在寻找的是能够使用我选择的任何ip更改该URL中的IP并获取该页面的源代码。

在Silverlight中提供错误的相同代码适用于其他网站:例如:http://tcpiputils.com/browse/ip-address/79.118.20.240http://livescore.com,但也不适用于google.com和其他网站。

1 个答案:

答案 0 :(得分:3)

您的代码有效,因为该站点(tcputils.com,livescore.com)在根目录中具有crossdomain.xml文件(例如,http://livescore.com/crossdomain.xml)。此文件基本上是一个选择,允许从外部网站域访问网站数据。

如果文件丢失(例如,在网站http://ip-whois-lookup.com/),则来自silverlight应用程序的请求不起作用。这是银光安全限制。

相关问题