Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

HttpResponseHeaderCollection class

Applies to Windows and Windows Phone

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

Syntax


var httpResponseHeaderCollection = headers;

Attributes

[DualApiPartition()]
[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06030000)]

Members

The HttpResponseHeaderCollection class has these types of members:

Methods

The HttpResponseHeaderCollection class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
Add(KeyValuePair) [C#, VB]Adds a new key-value pair to the HttpResponseHeaderCollection.
Add(String) [C#, VB]Adds an item to the HttpResponseHeaderCollection.
Append Adds a new item to the end of the HttpResponseHeaderCollection.
Clear Removes all objects from the collection.
Contains [C#, VB]Returns a value that indicates whether a specified key-value pair exists in the HttpResponseHeaderCollection.
ContainsKey [C#, VB]Returns a value that indicates whether a specified key exists in the HttpResponseHeaderCollection.
CopyTo [C#, VB]Copies the elements of the collection to an array, starting at a particular array index.
First [C++, JavaScript]Retrieves an iterator to the first item in the HttpResponseHeaderCollection.
GetView [C++, JavaScript]Returns an immutable view of the HttpResponseHeaderCollection.
HasKey [C++, JavaScript]Determines whether the HttpResponseHeaderCollection contains the specified key.
Insert [C++, JavaScript]Inserts or replaces an item in the HttpResponseHeaderCollection with the specified key and value.
Lookup [C++, JavaScript]Lookup an item in the HttpResponseHeaderCollection.
Remove [C++, JavaScript]Removes a specific object from the HttpResponseHeaderCollection.
Remove(KeyValuePair) [C#, VB]Removes a specific key-value pair from the HttpResponseHeaderCollection.
Remove(String) [C#, VB]Removes a specific item from the HttpResponseHeaderCollection.
ToString Returns a string that represents the current HttpResponseHeaderCollection object.
TryAppendWithoutValidation Try to append the specified item to the HttpResponseHeaderCollection without validation.
TryGetValue [C#, VB]Returns a value that indicates whether a specified key exists in the HttpResponseHeaderCollection. If an item with that key exists, the item is retrieved as an out parameter.

 

Properties

The HttpResponseHeaderCollection class has these properties.

PropertyAccess typeDescription

Age

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

Allow

Read-onlyGets the HttpMethodHeaderValueCollection of HttpMethod objects that represent the value of an Allow HTTP header on an HTTP response.

CacheControl

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

Connection

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

Count [C#, VB]

Read-onlyGets the number of elements contained in the collection.

Date

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

IsReadOnly [C#, VB]

Read-onlyGets a value indicating whether the dictionary is read-only.

Item [C#, VB]

Read/writeGets or sets the element value at the specified key index.

Keys [C#, VB]

Read-onlyGets an ICollection object containing the keys of the HttpResponseHeaderCollection.

Location

Read/writeGets or sets the Uri that represents the value or a Location HTTP header on an HTTP response.

ProxyAuthenticate

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

RetryAfter

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

Size [C++, JavaScript]

Read-onlyGets the number of objects in the HttpResponseHeaderCollection.

TransferEncoding

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

Values [C#, VB]

Read-onlyGets an ICollection object containing the values of the HttpResponseHeaderCollection .

WwwAuthenticate

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

 

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<T> with a KeyValuePair of String and String as the constraint.

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());
        }
        


Requirements

Minimum supported client

Windows 8.1 [Windows Store apps, desktop apps]

Minimum supported server

Windows Server 2012 R2 [Windows Store apps, desktop apps]

Minimum supported phone

Windows Phone 8.1 [Windows Phone Silverlight 8.1 and Windows Runtime apps]

Namespace

Windows.Web.Http.Headers
Windows::Web::Http::Headers [C++]

Metadata

Windows.winmd

See also

HttpResponseMessage
HttpResponseMessage.Headers
Object
IMap(String, String)
IIterable(IKeyValuePair)
IStringable

 

 

Show:
© 2014 Microsoft