2010-05-03 52 views
5

我试图使用Silverlight获取页面的html内容。 Web响应和请求类不能在silverlight中工作。使用Silverlight获取页面的html内容

我做了一些谷歌搜索,我发现了一些东西。这是我的尝试:

public partial class MainPage : UserControl 
{ 
    string result; 
    WebClient client; 

    public MainPage() 
    { 
    InitializeComponent(); 
    this.result = string.Empty; 
    this.client = new WebClient(); 
    this.client.DownloadStringCompleted += ClientDownloadStringCompleted; 
    } 

    private void btn1_Click(object sender, RoutedEventArgs e) 
    { 
    string url = "http://www.nu.nl/feeds/rss/algemeen.rss"; 

    this.client.DownloadStringAsync(new Uri(url, UriKind.Absolute)); 

    if (this.result != string.Empty && this.result != null) 
    { 
    this.txbSummery.Text = this.result; 
    } 
    } 

    private void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
    this.result = e.Result; 
    //handle the response. 
    } 
} 

它给了我一个运行时错误按下按钮后:

微软JScript运行时错误:在Silverlight应用程序处理错误的操作过程中出现的异常,使结果无效。检查异常详情的InnerException。在System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() 在System.Net.DownloadStringCompletedEventArgs.get_Result() 在JWTG.MainPage.ClientDownloadStringCompleted(对象发件人,DownloadStringCompletedEventArgs E) 在System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs E) 在System.Net.WebClient.DownloadStringOperationCompleted(Object arg)

我试过很多东西,但都失败了。

我在想什么?还是有人知道我能以不同的方式实现这一目标?

在此先感谢!

+0

你得到一个JScript错误。您发布的代码与错误无关。我刚刚尝试过你的代码片段,它很有用。顺便说一句:你必须将'this.txbSummery.Text = this.result;'移动到你的'ClientDownloadStringCompleted'方法中。目前,您正在尝试将文本放入文本框中,然后再下载。这显然不起作用。 – 2010-05-03 13:09:53

+0

看看:http://forums.silverlight.net/forums/t/54721.aspx也许可以帮助你。这个问题可能是配置错误的'web.config'文件。 – 2010-05-03 14:03:23

回答

1

这与clientaccesspolicy.xml相关。在这里阅读更多: http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx

"If the connection request was from a WebClient or an HTTP class to a cross-domain site, the Silverlight runtime tries to download the security policy file using the HTTP protocol. The Silverlight runtime first tries to download a Silverlight policy file with a name of "clientaccesspolicy.xml" at the root of the requested target domain using the HTTP protocol.

If the "clientaccesspolicy.xml" is either not found (the web request returns a 404 status code), returned with an unexpected mime-type, is not valid XML, or has an invalid root node, then the Silverlight runtime will issue a request for a for the Flash policy file with a name of "crossdomain.xml" at the root of the requested target domain, using the HTTP protocol.

HTTP redirects for the policy file are not allowed. A redirect for a policy file will result in a SecurityException of access denied."

+0

嗨, 我想通了。我尝试使用silverlight访问的网站没有策略文件。不管怎么说,还是要谢谢你! – Yustme 2010-05-20 20:46:51

0

在这一行

this.client.DownloadStringAsync(new Uri(url, UriKind.Absolute)); 

你,说明在后台线程的asynchroneous下载。在接下来的一行中,你会以某种方式期待它已经完成?

如果您对Threading没有任何了解,请先尝试使用DownloadString。那么你的代码将工作。

+0

嗨, 我试着在下载回调中放入'下一行',但是这并没有解决运行时错误。 我在哪里可以找到这个'do​​wnloadstring'?它属于哪一类? – Yustme 2010-05-03 13:02:23

+0

@Foxfire:你真的不想做一个阻塞呼叫来检索一个网页,是吗? Yustme应该使用'DownloadStringAsync',但将处理返回数据的逻辑放入'ClientDownloadStringCompleted'方法中。 – 2010-05-03 13:16:56

+0

@Yustme:你不能把事情放入事件中,因为它会从不同的线程调用。 – Foxfire 2010-05-03 14:27:58

1

试试这个,而不是你的btn1_ClickClientDownloadStringCompleted方法。它在下载提要后更新文本框后调用GUI线程。如果因网络错误而失败,它将解压该异常(包含在TargetInvocationException中的内部异常)并重新抛出内部异常。

private void btn1_Click(object sender, RoutedEventArgs e) 
{ 
    string url = "http://www.nu.nl/feeds/rss/algemeen.rss"; 

    this.client.DownloadStringAsync(new Uri(url)); 
} 

private void ClientDownloadStringCompleted(object sender, 
         DownloadStringCompletedEventArgs e) 
{ 
    try 
    { 
     Dispatcher.BeginInvoke(() => this.txbSummery.Text = e.Result ?? ""); 
    } 
    catch (TargetInvocationException tiex) 
    { 
     throw tiex.InnerException; 
    } 
} 

如果错误再次发生(我猜会发生),请在这里发布堆栈跟踪和错误消息。

+0

嗨, 不知怎的,我忽略了这个帖子:○ 这是我所得到的:在Silverlight应用程序 抛出新的错误(“未处理的错误操作过程中出现的异常,使结果无效检查的InnerException为异常的详细信息。 System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()\ n at System.Net.DownloadStringCompletedEventArgs.get_Result()\ n at JWTG.MainPage。<> c__DisplayClass2。 b__0()“); 有没有堆栈跟踪 – Yustme 2010-05-03 17:34:05

+0

该死的,再次装箱的例外。那么你也许可以看看innerException呢?使用调试器,或者只是用'throw tiex.InnerException.InnerException;'替换'throw tiex.InnerException;' – 2010-05-03 18:05:17

+0

它永远不会到达那么远,它停在Dispatcher.BeginInvoke(()=> this.txbSummery.Text = e .Result ??“”);但看到我的帖子下这一个进一步的信息 – Yustme 2010-05-03 20:20:08

1

你试试吧

private void btn1_Click(object sender, RoutedEventArgs e) 
    { 
     string url = "http://www.nu.nl/feeds/rss/algemeen.rss"; 

     this.client.DownloadStringAsync(new Uri(url, UriKind.Absolute)); 

    } 

    private void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     Stream s = e.Result; 
     StreamReader strReader = new StreamReader(s); 
     string webContent = strReader.ReadToEnd(); 
     s.Close(); 
     this.txbSummery.Text =webContent; 

    }