HttpResponseHeaderCollection HttpResponseHeaderCollection HttpResponseHeaderCollection HttpResponseHeaderCollection HttpResponseHeaderCollection Class

Definition

Provides a collection of the HTTP headers associated with an HTTP response.

public : sealed class HttpResponseHeaderCollection : IIterable<IKeyValuePair<Platform::String, Platform::String>>, IMap<Platform::String, Platform::String>, IStringable, IHttpResponseHeaderCollection
struct winrt::Windows::Web::Http::Headers::HttpResponseHeaderCollection : IIterable<IKeyValuePair<Platform::String, Platform::String>>, IMap<Platform::String, Platform::String>, IStringable, IHttpResponseHeaderCollection
public sealed class HttpResponseHeaderCollection : IEnumerable<KeyValuePair<String, String>>, IDictionary<String, String>, IStringable, IHttpResponseHeaderCollection
Public NotInheritable Class HttpResponseHeaderCollection Implements IEnumerable<KeyValuePair<String, String>>, IDictionary<String, String>, IStringable, IHttpResponseHeaderCollection
// This class does not provide a public constructor.
Attributes

Windows 10 requirements

Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

The following sample code shows a method to get and set response headers on an HttpResponseMessage object using the properties on the HttpResponseHeaderCollection object. The Windows.Web.Http.Headers namespace has a number of strongly-typed header collection and value classes for specific HTTP headers that can be used to get and set headers with validation.


using System;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Web.Http;
using Windows.Web.Http.Headers;

        void DemonstrateResponseHeaders() {

            DemonstrateHeaderResponseAge();
            DemonstrateHeaderResponseAllow();
            DemonstrateHeaderResponseCacheControl();
            DemonstrateHeaderResponseDate();
            DemonstrateHeaderResponseLocation();
            DemonstrateHeaderResponseProxyAuthenticate();

        }


        public void DemonstrateHeaderResponseAge()
        {
            var response = new HttpResponseMessage();

            // Set the header with a strong type.
            DateTimeOffset value = DateTimeOffset.UtcNow;
            response.Headers.Age = new TimeSpan(1, 35, 55); // 1 hour, 35 minutes, 55 seconds.

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("Age value in minutes: {0}", response.Headers.Age.Value.TotalMinutes);

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The Age ToString() results: {0}", response.Headers.Age.ToString());
        }


        public void DemonstrateHeaderResponseAllow()
        {
            var response = new HttpResponseMessage();

            // Set the header with a string
            response.Headers.Allow.TryParseAdd ("GET");

            // Set the header with a strong type
            response.Headers.Allow.Add(HttpMethod.Patch);

            // Get the strong type out
            foreach (var value in response.Headers.Allow)
            {
                System.Diagnostics.Debug.WriteLine("Allow value: {0}", value.Method);
            }

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The Allow ToString() results: {0}", response.Headers.Allow.ToString());
        }

        public void DemonstrateHeaderResponseCacheControl()
        {
            var response = new HttpResponseMessage();

            // Set the header with a string
            response.Headers.CacheControl.TryParseAdd("public");

            // Set the header with a strong type
            response.Headers.CacheControl.Add(new HttpNameValueHeaderValue("max-age", "30"));

            // Get the strong type out
            foreach (var value in response.Headers.CacheControl)
            {
                System.Diagnostics.Debug.WriteLine("CacheControl {0}={1}", value.Name, value.Value);
            }

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The CacheControl ToString() results: {0}", response.Headers.CacheControl.ToString());
        }

        public void DemonstrateHeaderResponseDate()
        {
            var response = new HttpResponseMessage();

            // Set the header with a strong type.
            response.Headers.Date = DateTimeOffset.UtcNow;

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("Date value in ticks: {0}", response.Headers.Date.Value.Ticks);

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The Date ToString() results: {0}", response.Headers.Date.ToString());
        }

        public void DemonstrateHeaderResponseLocation()
        {
            var response = new HttpResponseMessage();

            // Set the header with a strong type.
            response.Headers.Location = new Uri("http://example.com/");

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("Location absolute uri: {0}", response.Headers.Location.AbsoluteUri);

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The Location ToString() results: {0}", response.Headers.Location.ToString());
        }

        public void DemonstrateHeaderResponseProxyAuthenticate()
        {
            var response = new HttpResponseMessage();

            // Set the header with a strong type.
            response.Headers.ProxyAuthenticate.TryParseAdd("Basic");
            response.Headers.ProxyAuthenticate.Add(new HttpChallengeHeaderValue("authScheme", "authToken"));

            // Get the strong type out
            foreach (var value in response.Headers.ProxyAuthenticate)
            {
                System.Diagnostics.Debug.WriteLine("Proxy authenticate scheme and token: {0} {1}", value.Scheme, value.Token);
            }

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The ProxyAuthenticate ToString() results: {0}", response.Headers.ProxyAuthenticate.ToString());
        }

        public void DemonstrateHeaderResponseRetryAfter()
        {
            var response = new HttpResponseMessage();

            // Set the header with a strong type.
            HttpDateOrDeltaHeaderValue newvalue;
            bool parseOk = HttpDateOrDeltaHeaderValue.TryParse("", out newvalue);
            if (parseOk)
            {
                response.Headers.RetryAfter = newvalue;
            }

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("Date value in ticks: {0}", response.Headers.Date.Value.Ticks);

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The Date ToString() results: {0}", response.Headers.Date.ToString());
        }

Remarks

The HttpResponseHeaderCollection is a collection of the HTTP headers associated with an HTTP response to an HTTP request message. The HttpResponseHeaderCollection object can be used to get or set the specific headers on the HTTP response. Most of the properties on the HttpResponseHeaderCollection object provide access for the value of a specific HTTP header.

The Headers property on HttpResponseMessage returns an HttpResponseHeaderCollection object. This is how an HttpResponseHeaderCollection is constructed.

Enumerating the collection in C# or Microsoft Visual Basic

You can iterate through an HttpResponseHeaderCollection object in C# or Microsoft Visual Basic. In many cases, such as using foreach syntax, the compiler does this casting for you and you won't need to cast to IEnumerable explicitly. If you do need to cast explicitly, for example if you want to call GetEnumerator, cast the collection object to IEnumerable with a KeyValuePair of String and String as the constraint.

Properties

Age Age Age Age Age

Gets or sets the TimeSpan object that represents the value of an Age HTTP header on an HTTP response.

Allow Allow Allow Allow Allow

Gets the HttpMethodHeaderValueCollection of HttpMethod objects that represent the value of an Allow HTTP header on an HTTP response.

CacheControl CacheControl CacheControl CacheControl CacheControl

Gets the HttpCacheDirectiveHeaderValueCollection of objects that represent the value of a Cache-Control HTTP header on an HTTP response.

Connection Connection Connection Connection Connection

Gets the HttpConnectionOptionHeaderValueCollection of HttpConnectionOptionHeaderValue objects that represent the value of a Connection HTTP header on an HTTP response.

Date Date Date Date Date

Gets or sets the DateTime object that represents the value of a Date HTTP header on an HTTP response.

Item[TKey] Item[TKey] Item[TKey] Item[TKey] Item[TKey]

Gets or sets the element with the specified key.

Keys Keys Keys Keys Keys

Gets an System.Collections.Generic.ICollection`1 containing the keys of the System.Collections.Generic.IDictionary`2.

Location Location Location Location Location

Gets or sets the Uri that represents the value or a Location HTTP header on an HTTP response.

ProxyAuthenticate ProxyAuthenticate ProxyAuthenticate ProxyAuthenticate ProxyAuthenticate

Gets the HttpChallengeHeaderValueCollection of HttpChallengeHeaderValue objects that represent the value of a Proxy-Authenticate HTTP header on an HTTP response.

RetryAfter RetryAfter RetryAfter RetryAfter RetryAfter

Gets or sets the HttpDateOrDeltaHeaderValue object that represent the value of a Retry-After HTTP header on an HTTP response.

Size Size Size Size Size

Gets the number of objects in the HttpResponseHeaderCollection.

TransferEncoding TransferEncoding TransferEncoding TransferEncoding TransferEncoding

Gets the HttpTransferCodingHeaderValueCollection of HttpTransferCodingHeaderValue objects that represent the value of a Transfer-Encoding HTTP header on an HTTP response.

Values Values Values Values Values

Gets an System.Collections.Generic.ICollection`1 containing the values in the System.Collections.Generic.IDictionary`2.

WwwAuthenticate WwwAuthenticate WwwAuthenticate WwwAuthenticate WwwAuthenticate

Gets the HttpChallengeHeaderValueCollection of HttpChallengeHeaderValue objects that represent the value of a WWW-Authenticate HTTP header on an HTTP response.

Methods

Add(TKey, TValue) Add(TKey, TValue) Add(TKey, TValue) Add(TKey, TValue) Add(TKey, TValue)

Adds an element with the provided key and value to the System.Collections.Generic.IDictionary`2.

Append(String, String) Append(String, String) Append(String, String) Append(String, String) Append(String, String)

Adds a new item to the end of the HttpResponseHeaderCollection.

Clear() Clear() Clear() Clear() Clear()

Removes all objects from the collection.

ContainsKey(TKey) ContainsKey(TKey) ContainsKey(TKey) ContainsKey(TKey) ContainsKey(TKey)

Determines whether the System.Collections.Generic.IDictionary`2 contains an element with the specified key.

First() First() First() First() First()

Retrieves an iterator to the first item in the HttpResponseHeaderCollection.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

Returns an enumerator that iterates through the collection.

GetView() GetView() GetView() GetView() GetView()

Returns an immutable view of the HttpResponseHeaderCollection.

HasKey(String) HasKey(String) HasKey(String) HasKey(String) HasKey(String)

Determines whether the HttpResponseHeaderCollection contains the specified key.

Insert(String, String) Insert(String, String) Insert(String, String) Insert(String, String) Insert(String, String)

Inserts or replaces an item in the HttpResponseHeaderCollection with the specified key and value.

Lookup(String) Lookup(String) Lookup(String) Lookup(String) Lookup(String)

Lookup an item in the HttpResponseHeaderCollection.

Remove(T) Remove(T) Remove(T) Remove(T) Remove(T)

Removes the element with the specified key from the System.Collections.Generic.IDictionary`2.

Remove(String) Remove(String) Remove(String) Remove(String) Remove(String)

Removes an item with a given key from the HttpResponseHeaderCollection.

ToString() ToString() ToString() ToString() ToString()

Returns a string that represents the current HttpResponseHeaderCollection object.

TryAppendWithoutValidation(String, String) TryAppendWithoutValidation(String, String) TryAppendWithoutValidation(String, String) TryAppendWithoutValidation(String, String) TryAppendWithoutValidation(String, String)

Try to append the specified item to the HttpResponseHeaderCollection without validation.

TryGetValue(TKey, out TValue) TryGetValue(TKey, out TValue) TryGetValue(TKey, out TValue) TryGetValue(TKey, out TValue) TryGetValue(TKey, out TValue)

Gets the value associated with the specified key.

See Also