我试图使用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)
我试过很多东西,但都失败了。
我在想什么?还是有人知道我能以不同的方式实现这一目标?
在此先感谢!
你得到一个JScript错误。您发布的代码与错误无关。我刚刚尝试过你的代码片段,它很有用。顺便说一句:你必须将'this.txbSummery.Text = this.result;'移动到你的'ClientDownloadStringCompleted'方法中。目前,您正在尝试将文本放入文本框中,然后再下载。这显然不起作用。 – 2010-05-03 13:09:53
看看:http://forums.silverlight.net/forums/t/54721.aspx也许可以帮助你。这个问题可能是配置错误的'web.config'文件。 – 2010-05-03 14:03:23