Windows.Web.Syndication.SyndicationClient.SetRequestHeader C# (CSharp) Méthode

SetRequestHeader() public méthode

public SetRequestHeader ( [ name, [ value ) : void
name [
value [
Résultat void
		public extern void SetRequestHeader([In] string name, [In] string value);
		public extern IAsyncOperationWithProgress<SyndicationFeed, RetrievalProgress> RetrieveFeedAsync([In] Uri uri);

Usage Example

        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);
        }
All Usage Examples Of Windows.Web.Syndication.SyndicationClient::SetRequestHeader