C# 使用Silverlight获取页面的html内容

C# 使用Silverlight获取页面的html内容,c#,html,silverlight,runtime,C#,Html,Silverlight,Runtime,我正在尝试使用silverlight获取页面的html内容。Webresponse和request类在silverlight中不起作用 我在谷歌上搜索了一下,发现了一些东西。这就是我所尝试的: public partial class MainPage : UserControl { string result; WebClient client; public MainPage() { InitializeComponent(); this.result = s

我正在尝试使用silverlight获取页面的html内容。Webresponse和request类在silverlight中不起作用

我在谷歌上搜索了一下,发现了一些东西。这就是我所尝试的:

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.
  }
 }
按下按钮后,会出现运行时错误:

Microsoft JScript运行时错误:Silverlight应用程序中的未处理错误操作期间发生异常,导致结果无效。检查InnerException以了解异常详细信息。位于System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfEssential()处 在System.Net.DownloadStringCompletedEventArgs.get_Result()中 在JWTG.MainPage.ClientDownloadStringCompleted处完成(对象发送方,DownloadStringCompletedEventArgs e) 在System.Net.WebClient.OnDownloadStringCompleted(下载StringCompletedEventArgs e)中 在System.Net.WebClient.DownloadStringOperationCompleted(对象参数)处

我尝试过很多事情,但都失败了

我错过了什么?或者有人知道我如何以不同的方式实现这一点吗

提前谢谢

在这一行中

this.client.DownloadStringAsync(new Uri(url, UriKind.Absolute)); 
您正在后台线程中声明异步下载。在下一行中,你不知怎么地期望它已经完成了


如果您不了解线程,请先尝试下载字符串。然后你的代码就可以工作了。

试试这个方法,而不是你的
btn1\u单击
ClientDownloadStringCompleted
方法。它在下载提要后调用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;
    }
}

如果错误再次发生(我想会发生),请在此处发布stacktrace和错误消息。

这与clientaccesspolicy.xml有关。请在此处阅读更多信息:

如果连接请求是从WebClient或HTTP类到跨域站点,Silverlight运行时将尝试使用HTTP协议下载安全策略文件。Silverlight运行时首先尝试下载名为“clientaccesspolicy.xml”的Silverlight策略文件“使用HTTP协议在请求的目标域的根目录下

如果找不到“clientaccesspolicy.xml”(web请求返回404状态代码)、返回的mime类型不正确、xml无效或根节点无效,则Silverlight运行时将在请求的目标域的根节点上为Flash策略文件发出一个名为“crossdomain.xml”的请求,使用HTTP协议

不允许对策略文件进行HTTP重定向。策略文件的重定向将导致访问被拒绝的SecurityException。“

你试试看

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;

    }

嗨,我试着将“下一行”放在下载回调中,但这并没有修复运行时错误。我在哪里可以找到这个“downloadstring”?它属于什么类?@Foxfire:你不是真的想通过阻塞调用来检索网页,是吗?Yustme应该只使用
DownloadStringAsync
,但要使用handli的逻辑将返回的数据放入
ClientDownloadStringCompleted
方法中。@Yustme:您也不能在之后将其放入事件中,因为它将从另一个线程调用。@Philip:是的,我希望这样做-至少在他解决错误之前。他不能将当前逻辑放在那里,因为this.txbsumery正在运行在另一个线程上作为事件调用。您得到了一个JScript错误。您发布的代码与错误无关。我只是尝试了您的代码片段,它可以工作。顺便说一句:您必须将该
this.txbsumery.Text=this.result;
移动到
ClientDownloadStringCompleted
方法中。当前,您正在尝试将文本into下载之前先打开文本框。这显然不起作用。看看:也许这对你有帮助。问题可能是
web.config
文件配置错误。嗨,不知怎的,我忽略了这篇文章:o这是我得到的:抛出新错误(“Silverlight应用程序中未处理的错误操作过程中发生异常,导致结果无效。请检查InnerException以了解异常详细信息。在System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfEssential()\n在System.Net.DownloadStringCompletedEventArgs.get\u result()\n在JWTG.MainPage.c\u DisplayClass2.b\u 0())中);没有StackTraceDomain再次被装箱到异常中。那么,您可以看看innerException吗?使用调试器,或者将
throw tiex.innerException;
替换为
throw tiex.innerException.innerException;
它永远不会走那么远,它会停在Dispatcher.BeginInvoke()=>this.txbsumery.Text=e.Result???”;但请参阅我在这篇文章下的文章以了解更多信息。嗨,我发现了。我试图使用silverlight访问的网站没有策略文件。无论如何,谢谢!