【问题标题】:How to force WebClient to DownloadStringAsync every time on app launch每次应用启动时如何强制 WebClient 下载字符串异步
【发布时间】:2014-08-14 10:06:53
【问题描述】:

我正在尝试编写一个从远程 XML 文件读取数据的 RSS 阅读器,问题是当我第一次运行应用程序时它会正常下载,但是当我更新 XML 文件上的信息并再次运行应用程序时它不下载新项目。 我打电话给public Main()

WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri("http://suntvhost.do.am/sunnews.xml"));
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); 

处理程序:

private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //This simple test will return an error if the device is not connected to the internet
            if (e.Error != null)
                return;
            XElement xmlitems = XElement.Parse(e.Result);
            // We need to create a list of the elements
            List<XElement> elements = xmlitems.Descendants("item").ToList();
            // Now we're putting the informations that we got in our ListBox in the XAML code
            // we have to use a foreach statment to be able to read all the elements 
            // Description 1 , Link1 , Title1 are the attributes in the RSSItem class that I've already added
            List<RSSItem> aux = new List<RSSItem>();
            foreach (XElement rssItem in elements)
            {
                RSSItem rss = new RSSItem();
                rss.Description1 = rssItem.Element("description").Value;
                rss.Link1 = rssItem.Element("link").Value;
                rss.Title1 = rssItem.Element("title").Value;
                rss.Date1 = rssItem.Element("pubDate").Value;
                aux.Add(rss);

                TextBlock tbTitle = new TextBlock();
                tbTitle.Text = "\n" + rss.Title1;
                ListBoxRss.Items.Add(tbTitle);
             // and so on for the rest of items...
            }
        }

【问题讨论】:

    标签: c# windows-phone-8 webclient


    【解决方案1】:

    如果您获得相同的 RSS 文件,则这可能是由于客户端缓存造成的。
    通过添加这个 HTTP-Header 来强制 Windows Phone 下载 RSS 文件对我有用:

    wc.Headers[ HttpRequestHeader.IfModifiedSince ] = "Sat, 29 Oct 1994 19:43:31 GMT";
    

    【讨论】:

      【解决方案2】:

      您所要做的就是:

      string myUrl = "http://suntvhost.do.am/sunnews.xml?rand={0}";
      ...
      _webClient.DownloadStringAsync(new Uri(string.Format(myUrl, DateTime.Now.Ticks)));
      

      【讨论】:

        【解决方案3】:

        我将首先注册事件处理程序在您实际上尝试获得一些结果。注册该事件处理程序可能为时已晚:

        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
        wc.DownloadStringAsync(new Uri("http://suntvhost.do.am/sunnews.xml"));
        

        此外,仅在出现错误时返回并不便于诊断任何问题、将其写入日志文件等。

        【讨论】:

          【解决方案4】:

          问题是 WebClient 正在缓存数据。

          更多关于这里: C# WebClient disable cache

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多