Http​Request​Header​Collection Http​Request​Header​Collection Http​Request​Header​Collection Class

Definition

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

public sealed class HttpRequestHeaderCollection : IIterable, IMap, IStringable, IHttpRequestHeaderCollectionpublic sealed class HttpRequestHeaderCollection : IEnumerable, IDictionary, IStringable, IHttpRequestHeaderCollectionPublic NotInheritable Class HttpRequestHeaderCollection Implements IEnumerable, IDictionary, IStringable, IHttpRequestHeaderCollection
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

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

The Headers property on HttpRequestMessage returns an HttpRequestHeaderCollection object. The DefaultRequestHeaders property on HttpClient also returns an HttpRequestHeaderCollection object. These are the two methods to construct an HttpRequestHeaderCollection object.

The HttpRequestHeaderCollection represents HTTP headers on an HTTP request that an app developer can set, not all of the headers that may eventually be sent with the request. The HttpBaseProtocolFilter will add some additional headers.

Headers on an HTTP request can be changed by the underlying stack. This would be why an app might want to get a header value from the HttpRequestHeaderCollection after the request has completed.

Enumerating the collection in C# or Microsoft Visual Basic

You can iterate through an HttpRequestHeaderCollection 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 request headers on an HttpRequestMessage object using the properties on the HttpRequestHeaderCollection object. The Windows.Web.Http.Headers namespace also 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;

        public void DemonstrateHeaderRequest()
        {
            DemonstrateHeaderRequestAccept();
            DemonstrateHeaderRequestAcceptEncoding();
            DemonstrateHeaderRequestAcceptLanguage();
            DemonstrateHeaderRequestAuthorization();
            DemonstrateHeaderRequestCacheControl();
            DemonstrateHeaderRequestConnection();
            DemonstrateHeaderRequestCookie();
            DemonstrateHeaderRequestDate();
            DemonstrateHeaderRequestFrom();
            DemonstrateHeaderRequestHost();
            DemonstrateHeaderRequestIfModifiedSince();
            DemonstrateHeaderRequestIfUnmodifiedSince();
            DemonstrateHeaderRequestMaxForwards();
            DemonstrateHeaderRequestProxyAuthorization();
            DemonstrateHeaderRequestReferer();
            DemonstrateHeaderRequestUserAgent();
        }

        public void DemonstrateHeaderRequestAccept()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.Accept.TryParseAdd ("audio/*");
            parsedOk = request.Headers.Accept.TryParseAdd ("audio/*; q=0.2");
            parsedOk = request.Headers.Accept.TryParseAdd ("audio/*; q=0.4; mysetting=myvalue");

            // Set the header with a strong type.
            request.Headers.Accept.Add(new HttpMediaTypeWithQualityHeaderValue("audio/*", .6));

            // Get the strong type out
            foreach (var value in request.Headers.Accept)
            {
                System.Diagnostics.Debug.WriteLine("One of the Accept values: {0}={1}", value.MediaType, value.Quality);
            }

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

        public void DemonstrateHeaderRequestAcceptEncoding()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.AcceptEncoding.TryParseAdd("compress");
            parsedOk = request.Headers.AcceptEncoding.TryParseAdd("gzip;q=1.0");

            // Set the header with a strong type.
            request.Headers.AcceptEncoding.Add(new HttpContentCodingWithQualityHeaderValue("*", 0));

            // Get the strong type out
            foreach (var value in request.Headers.AcceptEncoding)
            {
                System.Diagnostics.Debug.WriteLine("One of the AcceptEncoding values: {0}={1}", value.ContentCoding, value.Quality);
            }

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

        public void DemonstrateHeaderRequestAcceptLanguage()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.AcceptLanguage.TryParseAdd("da");
            parsedOk = request.Headers.AcceptLanguage.TryParseAdd("en-gb;q=0.8");

            // Set the header with a strong type.
            request.Headers.AcceptLanguage.Add(new HttpLanguageRangeWithQualityHeaderValue("en", .7));

            // Get the strong type out
            foreach (var value in request.Headers.AcceptLanguage)
            {
                System.Diagnostics.Debug.WriteLine("One of the AcceptLanguage values: {0}={1}", value.LanguageRange, value.Quality);
            }

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

        public void DemonstrateHeaderRequestAuthorization()
        {
            var request = new HttpRequestMessage();

            // Set the header with a strong type.
            string username = "user";
            string password = "password";
            var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary (username + ":" + password, Windows.Security.Cryptography.BinaryStringEncoding.Utf8);
            string base64token = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(buffer);
            request.Headers.Authorization = new HttpCredentialsHeaderValue("Basic", base64token);


            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("One of the Authorization values: {0}={1}", 
                request.Headers.Authorization.Scheme,
                request.Headers.Authorization.Token);

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

        public void DemonstrateHeaderRequestCacheControl()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.CacheControl.TryParseAdd("no-store");

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

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

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


        public void DemonstrateHeaderRequestConnection()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.Connection.TryParseAdd("close");

            // Set the header with a strong type.
            request.Headers.Connection.Add(new HttpConnectionOptionHeaderValue("cache-control"));

            // Get the strong type out
            foreach (var value in request.Headers.Connection)
            {
                System.Diagnostics.Debug.WriteLine("One of the Connection values: {0}", value.Token);
            }

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

        public void DemonstrateHeaderRequestCookie()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.Cookie.TryParseAdd("cookieName=cookieValue");

            // Set the header with a strong type.
            request.Headers.Cookie.Add(new HttpCookiePairHeaderValue("cookie2", "value2"));

            // Get the strong type out
            foreach (var value in request.Headers.Cookie)
            {
                System.Diagnostics.Debug.WriteLine("One of the Cookie values: {0}={1}", value.Name, value.Value);
            }

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


        public void DemonstrateHeaderRequestDate()
        {
            var request = new HttpRequestMessage();

            // This is not typically set with a string.

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

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

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

        public void DemonstrateHeaderRequestFrom()
        {
            var request = new HttpRequestMessage();

            // Set the header with a string.
            request.Headers.From = "person@example.com";

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("From value: {0}", request.Headers.From);
        }

        public void DemonstrateHeaderRequestHost()
        {
            var request = new HttpRequestMessage();

            // This is not typically set with a string.

            // Set the header with a strong type.
            // HostName is in the Windows.Networking namespace.
            var value = new Windows.Networking.HostName("example.com");
            request.Headers.Host = value;

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("Canonical Host name: {0}", request.Headers.Host.CanonicalName);

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

        public void DemonstrateHeaderRequestIfModifiedSince()
        {
            var request = new HttpRequestMessage();

            // This is not typically set with a string.

            // Set the header with a strong type.
            var value = DateTimeOffset.Now.AddDays(-1); // Since yesterday.
            request.Headers.IfModifiedSince = value;

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

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

        public void DemonstrateHeaderRequestIfUnmodifiedSince()
        {
            var request = new HttpRequestMessage();

            // This is not typically set with a string.

            // Set the header with a strong type.
            var value = DateTimeOffset.Now.AddDays(-1); // Since yesterday.
            request.Headers.IfUnmodifiedSince = value;

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

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

        public void DemonstrateHeaderRequestMaxForwards()
        {
            var request = new HttpRequestMessage();

            // Set the header with an integer.
            request.Headers.MaxForwards= 2;

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("MaxForwards value: {0}", request.Headers.MaxForwards);
        }

        public void DemonstrateHeaderRequestProxyAuthorization()
        {
            var request = new HttpRequestMessage();

            // Set the header with a strong type.
            string username = "user";
            string password = "password";
            var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary(username + ":" + password, Windows.Security.Cryptography.BinaryStringEncoding.Utf16LE);
            string base64token = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(buffer);
            request.Headers.ProxyAuthorization = new HttpCredentialsHeaderValue("Basic", base64token);


            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("One of the ProxyAuthorixation values: {0}={1}",
                request.Headers.ProxyAuthorization.Scheme,
                request.Headers.ProxyAuthorization.Token);

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


        public void DemonstrateHeaderRequestReferer()
        {
            var request = new HttpRequestMessage();

            // This is not typically set with a string.

            // Set the header with a strong type.
            // Uri is either in the Windows.Foundation namespace (JavaScript and C++)
            // or in the System.Net namespace (C#).
            var value = new Uri("http://example.com/");
            request.Headers.Referer = value;

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

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

        public void DemonstrateHeaderRequestUserAgent()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.UserAgent.TryParseAdd("testprogram/1.0");

            // Set the header with a strong type.
            request.Headers.UserAgent.Add(new HttpProductInfoHeaderValue("myprogram", "2.2"));

            // Get the strong type out
            foreach (var value in request.Headers.UserAgent)
            {
                System.Diagnostics.Debug.WriteLine("One of the UserAgent values: {0} / {1}", value.Product.Name, value.Product.Version);
            }

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

Properties

Accept Accept Accept

Gets the HttpMediaTypeWithQualityHeaderValueCollection of HttpMediaTypeWithQualityHeaderValue objects that represent the value of an Accept HTTP header on an HTTP request.

public HttpMediaTypeWithQualityHeaderValueCollection Accept { get; }public HttpMediaTypeWithQualityHeaderValueCollection Accept { get; }Public ReadOnly Property Accept As HttpMediaTypeWithQualityHeaderValueCollection
Value
HttpMediaTypeWithQualityHeaderValueCollection HttpMediaTypeWithQualityHeaderValueCollection HttpMediaTypeWithQualityHeaderValueCollection

The collection of HttpMediaTypeWithQualityHeaderValue objects that represent the value of an Accept HTTP header. An empty collection means that the header is absent.

Attributes

AcceptEncoding AcceptEncoding AcceptEncoding

Gets the HttpContentCodingWithQualityHeaderValueCollection of HttpContentCodingWithQualityHeaderValue objects that represent the value of an Accept-Encoding HTTP header on an HTTP request.

public HttpContentCodingWithQualityHeaderValueCollection AcceptEncoding { get; }public HttpContentCodingWithQualityHeaderValueCollection AcceptEncoding { get; }Public ReadOnly Property AcceptEncoding As HttpContentCodingWithQualityHeaderValueCollection
Value
HttpContentCodingWithQualityHeaderValueCollection HttpContentCodingWithQualityHeaderValueCollection HttpContentCodingWithQualityHeaderValueCollection

The collection of HttpContentCodingWithQualityHeaderValue objects that represent the value of an Accept-Encoding HTTP header. An empty collection means that the header is absent.

Attributes

AcceptLanguage AcceptLanguage AcceptLanguage

Gets the HttpLanguageRangeWithQualityHeaderValueCollection of HttpLanguageRangeWithQualityHeaderValue objects that represent the value of an Accept-Language HTTP header on an HTTP request.

public HttpLanguageRangeWithQualityHeaderValueCollection AcceptLanguage { get; }public HttpLanguageRangeWithQualityHeaderValueCollection AcceptLanguage { get; }Public ReadOnly Property AcceptLanguage As HttpLanguageRangeWithQualityHeaderValueCollection
Value
HttpLanguageRangeWithQualityHeaderValueCollection HttpLanguageRangeWithQualityHeaderValueCollection HttpLanguageRangeWithQualityHeaderValueCollection

The collection of HttpLanguageRangeWithQualityHeaderValue objects that represent the value of an Accept-Language HTTP header. An empty collection means that the header is absent.

Attributes

Authorization Authorization Authorization

Gets or sets the HttpCredentialsHeaderValue object that represents the value of an Authorization HTTP header on an HTTP request.

public HttpCredentialsHeaderValue Authorization { get; set; }public HttpCredentialsHeaderValue Authorization { get; set; }Public ReadWrite Property Authorization As HttpCredentialsHeaderValue
Value
HttpCredentialsHeaderValue HttpCredentialsHeaderValue HttpCredentialsHeaderValue

The value of an Authorization HTTP header on an HTTP request. A null value means that the header is absent.

Attributes

CacheControl CacheControl CacheControl

Gets the HttpCacheDirectiveHeaderValueCollection that represents the value of a Cache-Control HTTP header on an HTTP request.

public HttpCacheDirectiveHeaderValueCollection CacheControl { get; }public HttpCacheDirectiveHeaderValueCollection CacheControl { get; }Public ReadOnly Property CacheControl As HttpCacheDirectiveHeaderValueCollection
Value
HttpCacheDirectiveHeaderValueCollection HttpCacheDirectiveHeaderValueCollection HttpCacheDirectiveHeaderValueCollection

The object that represents the value of a Cache-Control HTTP header. An empty collection means that the header is absent.

Attributes

Connection Connection Connection

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

public HttpConnectionOptionHeaderValueCollection Connection { get; }public HttpConnectionOptionHeaderValueCollection Connection { get; }Public ReadOnly Property Connection As HttpConnectionOptionHeaderValueCollection
Value
HttpConnectionOptionHeaderValueCollection HttpConnectionOptionHeaderValueCollection HttpConnectionOptionHeaderValueCollection

The collection of HttpConnectionOptionHeaderValue objects that represent the HTTP Connection header. An empty collection means that the header is absent.

Attributes

Cookie Cookie Cookie

Gets the HttpCookiePairHeaderValueCollection of HttpCookiePairHeaderValue objects that represent the value of an Cookie HTTP header sent on an HTTP request.

public HttpCookiePairHeaderValueCollection Cookie { get; }public HttpCookiePairHeaderValueCollection Cookie { get; }Public ReadOnly Property Cookie As HttpCookiePairHeaderValueCollection
Value
HttpCookiePairHeaderValueCollection HttpCookiePairHeaderValueCollection HttpCookiePairHeaderValueCollection

The collection of HttpCookiePairHeaderValue objects that represent the HTTP cookies in a Cookie HTTP header. An empty collection means that the header is absent.

Attributes

Date Date Date

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

public IReference<DateTime> Date { get; set; }public IReference<DateTime> Date { get; set; }Public ReadWrite Property Date As IReference<DateTime>
Value

The object that represents the value of a Date HTTP header on an HTTP request. A null value means that the header is absent.

Attributes

Remarks

The Date property represents the Date header on an HTTP request message. The Date header is the date and time the message was sent.

Javascript and .NET languages do not use the DateTime object directly. In Javascript a DateTime is projected as a object, and in .NET it is projected as a System.DateTimeOffset. Each language transparently handles the conversion to the granularity and date ranges for the respective language.

In C++, a value has the same granularity as a and supports the date ranges required by Javascript and .NET.

For more detailed information, see the Windows.Foundation.DateTime structure.

The following sample code shows a method to set the Date header on an HttpRequestMessage object using the Date property on the HttpRequestHeaderCollection object.

    public void DemonstrateHeaderRequestDate() {
        var request = new HttpRequestMessage();

        // This is not typically set with a string.

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

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

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

Expect Expect Expect

Gets the HttpExpectationHeaderValueCollection of HttpExpectationHeaderValue objects that represent the value of an Expect HTTP header on an HTTP request.

public HttpExpectationHeaderValueCollection Expect { get; }public HttpExpectationHeaderValueCollection Expect { get; }Public ReadOnly Property Expect As HttpExpectationHeaderValueCollection
Value
HttpExpectationHeaderValueCollection HttpExpectationHeaderValueCollection HttpExpectationHeaderValueCollection

The collection of HttpExpectationHeaderValue objects that represent the value of an Expect HTTP header on an HTTP request. An empty collection means that the header is absent.

Attributes

From From From

Gets or sets the String that represents the value of a From HTTP header on an HTTP request.

public string From { get; set; }public string From { get; set; }Public ReadWrite Property From As string
Value
string string string

The value of a From HTTP header on an HTTP request. An empty string means that the header is absent.

Attributes

Remarks

The following sample code shows a method to set the From header on an HttpRequestMessage object using the From property on the HttpRequestHeaderCollection object.

    public void DemonstrateHeaderRequestFrom() {
        var request = new HttpRequestMessage();

        // Set the header with a string.
        request.Headers.From = "person@example.com";

        // Get the strong type out
        System.Diagnostics.Debug.WriteLine("From value: {0}", request.Headers.From);
    }

Host Host Host

Gets or sets the HostName that represents the value of a Host HTTP header on an HTTP request.

public HostName Host { get; set; }public HostName Host { get; set; }Public ReadWrite Property Host As HostName
Value
HostName HostName HostName

The HostName that represents the value of a Host HTTP header on an HTTP request. A null value means that the header is absent.

Attributes

Remarks

The following sample code shows a method to set the Host header on an HttpRequestMessage object using the Host property on the HttpRequestHeaderCollection object.

    public void DemonstrateHeaderRequestHost() {
        var request = new HttpRequestMessage();

        // This is not typically set with a string.

        // Set the header with a strong type.
        // HostName is in the Windows.Networking namespace.
        var value = new Windows.Networking.HostName("example.com");
        request.Headers.Host = value;

        // Get the strong type out
        System.Diagnostics.Debug.WriteLine("Canonical Host name: {0}", request.Headers.Host.CanonicalName);

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

IfModifiedSince IfModifiedSince IfModifiedSince

Gets or sets the DateTime object that represents the value of an If-Modified-Since HTTP header on an HTTP request.

public IReference<DateTime> IfModifiedSince { get; set; }public IReference<DateTime> IfModifiedSince { get; set; }Public ReadWrite Property IfModifiedSince As IReference<DateTime>
Value

The DateTime object that represents the value of an If-Modified-Since HTTP header on an HTTP request. A null value means that the header is absent.

Attributes

Remarks

The IfModifiedSince property represents the value of an If-Modified-Since HTTP header on an HTTP request message. The If-Modified-Since header is the date and time the content was modified since.

Javascript and .NET languages do not use the DateTime object directly. In Javascript a DateTime is projected as a object, and in .NET it is projected as a System.DateTimeOffset. Each language transparently handles the conversion to the granularity and date ranges for the respective language.

In C++, a value has the same granularity as a and supports the date ranges required by Javascript and .NET.

For more detailed information, see the Windows.Foundation.DateTime structure.

The following sample code shows a method to set the If-Modified-Since header on an HttpRequestMessage object using the IfModifiedSince property on the HttpRequestHeaderCollection object.

    public void DemonstrateHeaderRequestIfModifiedSince() {
        var request = new HttpRequestMessage();

        // This is not typically set with a string.

        // Set the header with a strong type.
        var value = DateTimeOffset.Now.AddDays(-1); // Since yesterday.
        request.Headers.IfModifiedSince = value;

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

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

IfUnmodifiedSince IfUnmodifiedSince IfUnmodifiedSince

Gets or sets the DateTime object that represents the value of an If-Unmodified-Since HTTP header on an HTTP request.

public IReference<DateTime> IfUnmodifiedSince { get; set; }public IReference<DateTime> IfUnmodifiedSince { get; set; }Public ReadWrite Property IfUnmodifiedSince As IReference<DateTime>
Value

The DateTime object that represents the value of an If-Unmodified-Since HTTP header on an HTTP request. A null value means that the header is absent.

Attributes

Remarks

The IfUnmodifiedSince property represents the value of an If-Unmodified-Since HTTP header on an HTTP request message. The If-Unmodified-Since header is the date and time the content was not modified since.

Javascript and .NET languages do not use the DateTime object directly. In Javascript a DateTime is projected as a object, and in .NET it is projected as a System.DateTimeOffset. Each language transparently handles the conversion to the granularity and date ranges for the respective language.

In C++, a value has the same granularity as a and supports the date ranges required by Javascript and .NET.

For more detailed information, see the Windows.Foundation.DateTime structure.

The following sample code shows a method to set the If-Unmodified-Since header on an HttpRequestMessage object using the IfUnmodifiedSince property on the HttpRequestHeaderCollection object.

    public void DemonstrateHeaderRequestIfUnmodifiedSince() {
        var request = new HttpRequestMessage();

        // This is not typically set with a string.

        // Set the header with a strong type.
        var value = DateTimeOffset.Now.AddDays(-1); // Since yesterday.
        request.Headers.IfUnmodifiedSince = value;

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

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

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

Gets or sets the element with the specified key.

This member is not implemented in C++TValue this[TKey key] { get; set; }Property Item(key As TKey) As TValue
Parameters
key
TKey TKey TKey

The key of the element to get or set.

Value
TValue TValue TValue

The element with the specified key.

Attributes
See Also
System.Collections.Generic.ICollection<T>.IsReadOnlySystem.Collections.Generic.ICollection<T>.IsReadOnlySystem.Collections.Generic.ICollection<T>.IsReadOnly

Keys Keys Keys

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

This member is not implemented in C++ICollection<TKey> Keys { get; }ReadOnly Property Keys As ICollection(Of TKey)
Value
System.Collections.Generic.ICollection<TKey> System.Collections.Generic.ICollection<TKey> System.Collections.Generic.ICollection<TKey>

An System.Collections.Generic.ICollection`1 containing the keys of the object that implements System.Collections.Generic.IDictionary`2.

Attributes
See Also
System.Collections.Generic.ICollection<T>System.Collections.Generic.ICollection<T>System.Collections.Generic.ICollection<T>

MaxForwards MaxForwards MaxForwards

Gets or sets an integer value that represents the value of a Max-Forwards HTTP header on an HTTP request.

public IReference<uint> MaxForwards { get; set; }public IReference<uint> MaxForwards { get; set; }Public ReadWrite Property MaxForwards As IReference<uint>
Value

An integer value that represents the value of a Max-Forwards HTTP header on an HTTP request. A null value means that the header is absent.

Attributes

Remarks

The following sample code shows a method to set the Max-Forwards header on an HttpRequestMessage object using the MaxForwards property on the HttpRequestHeaderCollection object.

    public void DemonstrateHeaderRequestMaxForwards() {
        var request = new HttpRequestMessage();

        // Set the header with an integer.
        request.Headers.MaxForwards= 2;

        // Get the strong type out
        System.Diagnostics.Debug.WriteLine("MaxForwards value: {0}", request.Headers.MaxForwards);
    }

ProxyAuthorization ProxyAuthorization ProxyAuthorization

Gets or sets the HttpCredentialsHeaderValue object that represent the value of a Proxy-Authorization HTTP header on an HTTP request.

public HttpCredentialsHeaderValue ProxyAuthorization { get; set; }public HttpCredentialsHeaderValue ProxyAuthorization { get; set; }Public ReadWrite Property ProxyAuthorization As HttpCredentialsHeaderValue
Value
HttpCredentialsHeaderValue HttpCredentialsHeaderValue HttpCredentialsHeaderValue

The object that represent the value of a Proxy-Authorization HTTP header on an HTTP request. A null value means that the header is absent.

Attributes

Referer Referer Referer

Gets or sets the Uri that represents the value of a Referer HTTP header on an HTTP request.

public Uri Referer { get; set; }public Uri Referer { get; set; }Public ReadWrite Property Referer As Uri
Value
Uri Uri Uri

The object that represents the value of a Referer HTTP header on an HTTP request. A null value means that the header is absent.

Attributes

Remarks

The following sample code shows a method to set the Referer header on an HttpRequestMessage object using the Referer property on the HttpRequestHeaderCollection object.

    public void DemonstrateHeaderRequestReferer() {
        var request = new HttpRequestMessage();

        // This is not typically set with a string.

        // Set the header with a strong type.
        // Uri is either in the Windows.Foundation namespace (JavaScript and C++)
        // or in the System.Net namespace (C#).
        var value = new Uri("http://example.com/");
        request.Headers.Referer = value;

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

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

Size Size Size

Gets the number of objects in the HttpRequestHeaderCollection.

public uint Size { get; }This member is not implemented in C#This member is not implemented in VB.Net
Value
uint uint uint

The number of objects in the HttpRequestHeaderCollection.

Attributes

Remarks

The Size property returns the true number of items.

If you are programming using C# or Microsoft Visual Basic, the equivalent method is Count.

TransferEncoding TransferEncoding TransferEncoding

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

public HttpTransferCodingHeaderValueCollection TransferEncoding { get; }public HttpTransferCodingHeaderValueCollection TransferEncoding { get; }Public ReadOnly Property TransferEncoding As HttpTransferCodingHeaderValueCollection
Value
HttpTransferCodingHeaderValueCollection HttpTransferCodingHeaderValueCollection HttpTransferCodingHeaderValueCollection

The collection of HttpTransferCodingHeaderValue objects that represent the value of a Transfer-Encoding HTTP header on an HTTP request. An empty collection means that the header is absent.

Attributes

Remarks

The only transfer encoding value supported by HttpClient is chunked, even though the HttpTransferCodingHeaderValueCollection of HttpTransferCodingHeaderValue objects supports setting other values.

There is no need to set the transfer encoding value manually to chunked if the HttpRequestMessage is sent as an with no content-length specified or available using the method on HttpClient or one of the methods on the HTTP content classes.

The following sample code shows a method to set the Transfer-Encoding header on an HttpRequestMessage object using the TransferEncoding property on the HttpRequestHeaderCollection object.

    void DemoTransferEncoding(HttpRequestMessage m) {
        var h = m.Headers;

        uiLog.Text += "\nTRANSFERENCODING HEADER\n";
        // Transfer-Encoding: chunked
        var okTryParseAdd = h.TransferEncoding.TryParseAdd("chunked");
        okTryParseAdd = h.TransferEncoding.TryParseAdd("mini; a=b; c=d; e=f");
        h.TransferEncoding.Add(new HttpTransferCodingHeaderValue("cab"));
        h.TransferEncoding.TryParseAdd("newtype, othernewtype");

        // TransferEncoding is a HttpTransferCodingHeaderValueCollection
        // A collection of HttpTransferCodingHeaderValue

        // HttpTransferCodingHeaderValue has three items:
        // Value (string); e.g. "compress"
        // Parameter (IList<HttpNameValueHeaderValue>)
        //

        foreach (var item in h.TransferEncoding) {
            // item has: Value (string), Parameter IList<HttpNameValueHeaderValue>
            var parameterString = "";
            foreach (var parameter in item.Parameters) {
                parameterString += string.Format("[{0}={1}] ", parameter.Name, parameter.Value);
            }
            if (parameterString == "") {
                parameterString = "(no parameters)";
            } 
            uiLog.Text += string.Format("Value: {0} Parameters: {1} ToString: {2}\n", item.Value, parameterString, item.ToString());
        }
        uiLog.Text += string.Format("TransferEncoding: ToString: {0}\n\n", h.TransferEncoding.ToString());
    }

UserAgent UserAgent UserAgent

Gets the HttpProductInfoHeaderValueCollection of HttpProductInfoHeaderValue objects that represent the value of a User-Agent HTTP header on an HTTP request.

public HttpProductInfoHeaderValueCollection UserAgent { get; }public HttpProductInfoHeaderValueCollection UserAgent { get; }Public ReadOnly Property UserAgent As HttpProductInfoHeaderValueCollection
Value
HttpProductInfoHeaderValueCollection HttpProductInfoHeaderValueCollection HttpProductInfoHeaderValueCollection

The collection of HttpProductInfoHeaderValue objects that represent the value of a User-Agent HTTP header on an HTTP request. An empty collection means that the header is absent.

Attributes

Remarks

The following sample code shows a method to set the User-Agent header on an HttpRequestMessage object using the UserAgent property on the HttpRequestHeaderCollection object.

    void DemoUserAgent(HttpRequestMessage m) {
        var h = m.Headers;

        uiLog.Text += "\nUSERAGENT HEADER\n";
        // User-Agent: CERN-LineMode/2.15 libwww/2.17b3
        // User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Zune 4.0; InfoPath.3; MS-RTC LM 8; .NET4.0C; .NET4.0E)
        var ok = h.UserAgent.TryParseAdd("ie");
        ok = h.UserAgent.TryParseAdd("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Zune 4.0; InfoPath.3; MS-RTC LM 8; .NET4.0C; .NET4.0E)");
        ok = h.UserAgent.TryParseAdd("CERN-LineMode/2.15 libwww/2.17b3");
        h.UserAgent.Add (new HttpProductInfoHeaderValue ("productComment"));
        h.UserAgent.Add(new HttpProductInfoHeaderValue("Mozilla", "1.0"));

        // UserAgent is a HttpProductInfoHeaderValueCollection
        // A collection of HttpProductInfoHeaderValue

        // HttpProductInfoHeaderValue has two items:
        // Product (string)
        // Comment (string)
        //
        // According to the RFC, each "item" is EITHER a product OR a comment, and a product is EITHER product or product/version

        foreach (var item in h.UserAgent) {
            // item has: Value (string), Parameter IList<HttpNameValueHeaderValue>

            uiLog.Text += string.Format("Product: {0} Comment: {1} ToString: {2}\n", item.Product, item.Comment, item.ToString());
        }
        uiLog.Text += string.Format("UserAgent: ToString: {0}\n\n", h.UserAgent.ToString());
    }

Values Values Values

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

This member is not implemented in C++ICollection<TValue> Values { get; }ReadOnly Property Values As ICollection(Of TValue)
Value
System.Collections.Generic.ICollection<TValue> System.Collections.Generic.ICollection<TValue> System.Collections.Generic.ICollection<TValue>

An System.Collections.Generic.ICollection`1 containing the values in the object that implements System.Collections.Generic.IDictionary`2.

Attributes
See Also
System.Collections.Generic.ICollection<T>System.Collections.Generic.ICollection<T>System.Collections.Generic.ICollection<T>

Methods

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.

This member is not implemented in C++void Add(TKey key, TValue value)Sub Add(key As TKey, value As TValue)
Parameters
key
TKey TKey TKey

The object to use as the key of the element to add.

value
TValue TValue TValue

The object to use as the value of the element to add.

Attributes
See Also
System.Collections.Generic.ICollection<T>.IsReadOnlySystem.Collections.Generic.ICollection<T>.IsReadOnlySystem.Collections.Generic.ICollection<T>.IsReadOnly

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

Adds a new item to the end of the HttpRequestHeaderCollection.

public void Append(String name, String value)public void Append(String name, String value)Public Function Append(name As String, value As String) As void
Parameters
name
System.String System.String System.String

The name of the value to add.

value
System.String System.String System.String

The item value to add.

Attributes
See Also

Clear() Clear() Clear()

Removes all objects from the HttpRequestHeaderCollection.

public void Clear()This member is not implemented in C#This member is not implemented in VB.Net
Attributes

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

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

This member is not implemented in C++bool ContainsKey(TKey key)Function ContainsKey(key As TKey) As Boolean
Parameters
key
TKey TKey TKey

The key to locate in the System.Collections.Generic.IDictionary`2.

Returns
System.Boolean System.Boolean System.Boolean

true if the System.Collections.Generic.IDictionary`2 contains an element with the key; otherwise, false.

Attributes

First() First() First()

Retrieves an iterator to the first item in the HttpRequestHeaderCollection.

public IIterator<IKeyValuePair<string, string>> First()This member is not implemented in C#This member is not implemented in VB.Net
Returns

An object that can be used to enumerate the items in the collection. The iterator points to the first item in the HttpRequestHeaderCollection.

Attributes

GetEnumerator() GetEnumerator() GetEnumerator()

Returns an enumerator that iterates through the collection.

This member is not implemented in C++IEnumerator<T> GetEnumerator()Function GetEnumerator As IEnumerator(Of T)
Returns
System.Collections.Generic.IEnumerator<T> System.Collections.Generic.IEnumerator<T> System.Collections.Generic.IEnumerator<T>

An enumerator that can be used to iterate through the collection.

Attributes
See Also
System.Collections.Generic.IEnumerator<T>System.Collections.Generic.IEnumerator<T>System.Collections.Generic.IEnumerator<T>

GetView() GetView() GetView()

Returns an immutable view of the HttpRequestHeaderCollection.

public IMapView<string, string> GetView()This member is not implemented in C#This member is not implemented in VB.Net
Returns

The view of the HttpRequestHeaderCollection.

Attributes

Remarks

When programming with .NET, this method is hidden.

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

Determines whether the HttpRequestHeaderCollection contains the specified key.

public bool HasKey(String key)This member is not implemented in C#This member is not implemented in VB.Net
Parameters
key
System.String System.String System.String

The key associated with the item to locate.

Returns
bool bool bool

true if the key is found; otherwise, false.

Attributes

Remarks

When programming with .NET, this method is hidden and developers should use ContainsKey.

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

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

public bool Insert(String key, String value)This member is not implemented in C#This member is not implemented in VB.Net
Parameters
key
System.String System.String System.String

The key of the item to be inserted.

value
System.String System.String System.String

The value of the item to insert.

Returns
bool bool bool

true if an item with the specified key is an existing item that was replaced; otherwise false.

Attributes

Remarks

When programming with .NET, this method is hidden and developers should use the Add methods.

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

Finds an item in the HttpRequestHeaderCollection if it exists.

public string Lookup(String key)This member is not implemented in C#This member is not implemented in VB.Net
Parameters
key
System.String System.String System.String

The key of the item to lookup.

Returns
string string string

The value, if an item with the specified key exists. Use the HasKey method to determine whether the key exists.

Attributes

Remarks

Use the HasKey method to determine whether the key exists in the HttpRequestHeaderCollection.

When programming with .NET, this method is hidden and developers should use the Item indexer.

Remove(TKey) Remove(TKey) Remove(TKey)

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

This member is not implemented in C++bool Remove(TKey key)Function Remove(key As TKey) As Boolean
Parameters
key
TKey TKey TKey

The key of the element to remove.

Returns
System.Boolean System.Boolean System.Boolean

true if the element is successfully removed; otherwise, false. This method also returns false if key was not found in the original System.Collections.Generic.IDictionary`2.

Attributes
See Also
System.Collections.Generic.ICollection<T>.IsReadOnlySystem.Collections.Generic.ICollection<T>.IsReadOnlySystem.Collections.Generic.ICollection<T>.IsReadOnly

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

Removes a specific object from the HttpRequestHeaderCollection.

public void Remove(String key)This member is not implemented in C#This member is not implemented in VB.Net
Parameters
key
System.String System.String System.String

The key of the item to remove.

Attributes

ToString() ToString() ToString()

Returns a string that represents the current HttpRequestHeaderCollection object.

public string ToString()public string ToString()Public Function ToString() As string
Returns
string string string

A string that represents the current object.

Attributes

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

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

public bool TryAppendWithoutValidation(String name, String value)public bool TryAppendWithoutValidation(String name, String value)Public Function TryAppendWithoutValidation(name As String, value As String) As bool
Parameters
name
System.String System.String System.String

The name of the item to append.

value
System.String System.String System.String

The value of the item to append.

Returns
bool bool bool

true if the item was appended; otherwise false.

Attributes

Remarks

The TryAppendWithoutValidation method is available when you need to work with an HTTP header on an HTTP request that doesn't have a strongly-typed class for the HTTP header. If there is a strongly-typed implementation of the HTTP header, then the methods and properties on the strongly-typed class should be used instead of the TryAppendWithoutValidation method.

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

Gets the value associated with the specified key.

This member is not implemented in C++bool TryGetValue(TKey key, out TValue value)Function TryGetValue(key As TKey, ByRef value As TValue) As Boolean
Parameters
key
TKey TKey TKey

The key whose value to get.

value
TValue TValue TValue

When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

Returns
System.Boolean System.Boolean System.Boolean

true if the object that implements System.Collections.Generic.IDictionary`2 contains an element with the specified key; otherwise, false.

Attributes