public SetRequestHeader ( [ name, [ value ) : void | ||
name | [ | |
value | [ | |
return | void |
public async Task RefreshAsync(string url) { Uri uri; if (!Uri.TryCreate(url, UriKind.Absolute, out uri)) return; bi.IsActive = true; string errorMessage = null; var client = new SyndicationClient(); client.BypassCacheOnRetrieve = true; // Although most HTTP servers do not require User-Agent header, // others will reject the request or return a different response if this header is missing. // Use SetRequestHeader() to add custom headers. client.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); try { var feed = await client.RetrieveFeedAsync(uri); var ds = feed.Items.Select(item => new RssItem() { Url = item.Links.Count > 0 ? item.Links[0].Uri.AbsoluteUri : string.Empty, Title = item.Title != null ? item.Title.Text : "(no title)", Author = item.Authors.Count > 0 ? item.Authors[0].NodeValue : "(no author)", Published = item.PublishedDate.DateTime, Content = item.Content != null ? item.Content.Text : (item.Summary != null ? item.Summary.Text : "(no content)") }); gvRss.ItemsSource = ds; } catch (Exception ex) { //SyndicationErrorStatus status = SyndicationError.GetStatus(ex.HResult); //if (status == SyndicationErrorStatus.InvalidXml) // OutputField.Text += "An invalid XML exception was thrown. Please make sure to use a URI that points to a RSS or Atom feed."; //if (status == SyndicationErrorStatus.Unknown) //{ // WebErrorStatus webError = WebError.GetStatus(ex.HResult); // if (webError == WebErrorStatus.Unknown) // { // // Neither a syndication nor a web error. Rethrow. // throw; // } //} errorMessage = ex.Message; } bi.IsActive = false; if (!string.IsNullOrEmpty(errorMessage)) await Utils.MessageBox(errorMessage); }
private async void GetFeed_Click(object sender, RoutedEventArgs e) { OutputField.Text = ""; // By default 'FeedUri' is disabled and URI validation is not required. When enabling the text box // validating the URI is required since it was received from an untrusted source (user input). // The URI is validated by calling Uri.TryCreate() that will return 'false' for strings that are not valid URIs. // Note that when enabling the text box users may provide URIs to machines on the intrAnet that require // the "Home or Work Networking" capability. Uri uri; if (!Uri.TryCreate(FeedUri.Text.Trim(), UriKind.Absolute, out uri)) { rootPage.NotifyUser("Error: Invalid URI.", NotifyType.ErrorMessage); return; } SyndicationClient client = new SyndicationClient(); client.BypassCacheOnRetrieve = true; // Although most HTTP servers do not require User-Agent header, others will reject the request or return // a different response if this header is missing. Use SetRequestHeader() to add custom headers. client.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); rootPage.NotifyUser("Downloading feed...", NotifyType.StatusMessage); OutputField.Text = "Downloading feed: " + uri.ToString() + "\r\n"; try { currentFeed = await client.RetrieveFeedAsync(uri); rootPage.NotifyUser("Feed download complete.", NotifyType.StatusMessage); DisplayFeed(); } catch (Exception ex) { SyndicationErrorStatus status = SyndicationError.GetStatus(ex.HResult); if (status == SyndicationErrorStatus.InvalidXml) { OutputField.Text += "An invalid XML exception was thrown. " + "Please make sure to use a URI that points to a RSS or Atom feed."; } if (status == SyndicationErrorStatus.Unknown) { WebErrorStatus webError = WebError.GetStatus(ex.HResult); if (webError == WebErrorStatus.Unknown) { // Neither a syndication nor a web error. Rethrow. throw; } } rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage); } }
/// <summary> /// Get the feeds with the SyndicationClient /// </summary> /// <param name="feedUri"></param> /// <returns></returns> public async Task<List<FeedItem>> GetFeedAsync(string url) { Uri uri; if (!Uri.TryCreate(url.Trim(), UriKind.Absolute, out uri)) { await ApiHelper.ShowMessageDialog("Error", "Invalid URI"); return null; } var client = new SyndicationClient(); client.BypassCacheOnRetrieve = true; client.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); try { var feed = await client.RetrieveFeedAsync(uri); return feed.Items.Select((item, i) => CreateFeedItem(item, feed.SourceFormat, i)).ToList(); } catch (Exception ex) { var status = SyndicationError.GetStatus(ex.HResult); if (status == SyndicationErrorStatus.InvalidXml) { await ApiHelper.ShowMessageDialog("Error", "An invalid XML exception was thrown. Please make sure to use a URI that points to a RSS or Atom feed." + "\n\n\n" + ex.Message); } if (status == SyndicationErrorStatus.Unknown) { WebErrorStatus webError = WebError.GetStatus(ex.HResult); if (webError == WebErrorStatus.Unknown) { await ApiHelper.ShowMessageDialog("Error", ex.Message + "\n" + ex.InnerException?.Message); } } } return null; }
public async void Refresh(string url) { string errorMessage = null; Uri uri; if (!Uri.TryCreate(url, UriKind.Absolute, out uri)) return; SyndicationClient client = new SyndicationClient(); client.BypassCacheOnRetrieve = true; // Although most HTTP servers do not require User-Agent header, others will reject the request or return // a different response if this header is missing. Use SetRequestHeader() to add custom headers. client.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); try { currentFeed = await client.RetrieveFeedAsync(uri); DisplayFeed(); } catch (Exception ex) { //SyndicationErrorStatus status = SyndicationError.GetStatus(ex.HResult); //if (status == SyndicationErrorStatus.InvalidXml) // OutputField.Text += "An invalid XML exception was thrown. Please make sure to use a URI that points to a RSS or Atom feed."; //if (status == SyndicationErrorStatus.Unknown) //{ // WebErrorStatus webError = WebError.GetStatus(ex.HResult); // if (webError == WebErrorStatus.Unknown) // { // // Neither a syndication nor a web error. Rethrow. // throw; // } //} errorMessage = ex.Message; } if (!string.IsNullOrEmpty(errorMessage)) await Utils.MessageBox(errorMessage); }
private static async Task<SyndicationFeed> GetMSDNBlogFeed() { SyndicationFeed feed = null; try { // Create a syndication client that downloads the feed. var client = new SyndicationClient(); client.BypassCacheOnRetrieve = true; client.SetRequestHeader(customHeaderName, customHeaderValue); // Download the feed. feed = await client.RetrieveFeedAsync(new Uri(feedUrl)); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } return feed; }
private static async Task<FeedData> RetrieveFeedAsync(Uri feedLink) { var feed = new SyndicationFeed(); var client = new SyndicationClient(); var feedData = new FeedData(); client.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); client.BypassCacheOnRetrieve = true; try { feed = await client.RetrieveFeedAsync(feedLink); } catch (Exception e) { SyndicationErrorStatus syndicationError = SyndicationError.GetStatus(e.HResult); if (syndicationError == SyndicationErrorStatus.Unknown) { WebErrorStatus webError = WebError.GetStatus(e.HResult); if (webError == WebErrorStatus.Unknown) { throw; } } return null; } if (feed.Title != null && !string.IsNullOrEmpty(feed.Title.Text)) { feedData.Title = feed.Title.Text; } if (feed.Subtitle != null && !string.IsNullOrEmpty(feed.Subtitle.Text)) { feedData.Description = feed.Subtitle.Text; } feedData.Link = feedLink; if (feed.Items != null && feed.Items.Any()) { feedData.PubDate = feed.Items.First().PublishedDate.DateTime; foreach (SyndicationItem item in feed.Items) { feedData.Items.Add(item.ToFeedItem(feed.SourceFormat)); } } return feedData; }