async Task <SyndicationFeed> ReadFromWebWithAlternative() { // including user agent, otherwise FB rejects the request var _Client = new HttpClient(); var _UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"; _Client.DefaultRequestHeaders.Add("user-agent", _UserAgent); // fetch as string to avoid error var _Uri = new Uri(this.SourceUrl); var _Response = await _Client.GetAsync(_Uri); var _String = await _Response.Content.ReadAsStringAsync(); // convert to xml (will validate, too) var _XmlDocument = new Windows.Data.Xml.Dom.XmlDocument(); _XmlDocument.LoadXml(_String); // manually fill feed from xml var _Feed = new Windows.Web.Syndication.SyndicationFeed(); _Feed.LoadFromXml(_XmlDocument); return(_Feed); }
public async void Run(IBackgroundTaskInstance taskInstance) { Logger.Log("Starting task"); //it is an async work that could take long, we need to get a deferral... var deferral = taskInstance.GetDeferral(); try { CachedFileController fileController = new CachedFileController(); fileController.NotifyMessage += (o,message) => { ShowNotificationBadge(message); }; var asyncDownload= fileController.DownloadFileAsync(); asyncDownload.Progress = (o, p) => { taskInstance.Progress = (uint)(p.BytesReceived * 100 / (p.TotalBytesToReceive ?? (50 * 1024))); }; await asyncDownload; //now count feed posts var file = await fileController.GetFileAsync(); XmlDocument doc = new XmlDocument(); doc.LoadXml(file); SyndicationFeed feed = new SyndicationFeed(); feed.LoadFromXml(doc); ShowNotificationBadge(feed.Items.Count); } catch (Exception ex) { Logger.Log("Download File Exception: {0}", ex.Message); ShowNotificationBadge("error"); ShowNotificationBadge(0); } finally { Logger.Log("End Task"); deferral.Complete(); } }
/// <summary> /// The timer callback for periodic updates /// </summary> /// <param name="state">A reference to the IoC <see cref="Container"/></param> private async void Callback(object state) { var container = state as Container; if (container == null) { return; } var viewModel = container.ResolveNamed<NewsViewModel>(NewsViewModel.Name); if (viewModel == null) { return; } var url = string.Empty; lock (m_Lock) { url = string.Format("http://www.reddit.com/r/{0}/new/.rss", m_Subreddit); } var feed = new SyndicationFeed(); var client = new HttpClient(); var rssString = await client.GetStringAsync(url); var xmlDocument = new XmlDocument(); xmlDocument.LoadXml(rssString); feed.LoadFromXml(xmlDocument); DispatcherHelper.RunOnUIThread(() => { viewModel.FeedItems = feed.Items; }); }