HttpContentHeaderCollection HttpContentHeaderCollection HttpContentHeaderCollection HttpContentHeaderCollection Class

Provides a collection of the HTTP headers associated with content on an HTTP request or response.

Syntax

Declaration

public sealed class HttpContentHeaderCollectionpublic sealed class HttpContentHeaderCollectionPublic NotInheritable Class HttpContentHeaderCollection

Remarks

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

The HttpContentHeaderCollection has a constructor and also is returned by the property on HttpBufferContent, HttpFormUrlEncodedContent, HttpMultipartContent, HttpMultipartFormDataContent, HttpStreamContent, and HttpStringContent, classes and the IHttpContent interface.

Enumerating the collection in C# or Microsoft Visual Basic

You can iterate through an HttpContentHeaderCollection 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 headers on HTTP content using the properties on the HttpContentHeaderCollection 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 Windows.Web.Http;
using Windows.Web.Http.Headers;

        public void DemonstrateContentHeader()
        {
            DemonstrateHeaderContentContentDisposition(); 
        }

        public void DemonstrateHeaderContentContentDisposition()
        {
            var content = new HttpStringContent("");
            bool parsedOk = false;

            // Set the header with a string.
            HttpContentDispositionHeaderValue value;
            parsedOk = HttpContentDispositionHeaderValue.TryParse("attachment; filename=\"fname.ext\"", out value);
            content.Headers.ContentDisposition = value;

            // Set the header with a strong type.

            content.Headers.ContentDisposition = new HttpContentDispositionHeaderValue("attachment");
            content.Headers.ContentDisposition.FileName = "myfile.exe";


            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("ContentDisposition filename: {0}={1}", 
                content.Headers.ContentDisposition.DispositionType,
                content.Headers.ContentDisposition.FileName);

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

Constructors summary

Initializes a new instance of the HttpContentHeaderCollection class.

Properties summary

Gets or sets the HttpContentDispositionHeaderValue object that represents the value of an HTTP Content-Disposition header on the HTTP content.

Gets the HttpContentCodingHeaderValueCollection of HttpContentCodingHeaderValue objects that represent the value of an HTTP Content-Encoding header on the HTTP content.

Gets the HttpLanguageHeaderValueCollection of objects that represent the value of an HTTP Content-Language header on the HTTP content.

Gets or sets the value of the HTTP Content-Length header on the HTTP content.

Gets or sets the value of the HTTP Content-Location header on the HTTP content.

Gets or sets the value of an HTTP Content-MD5 header on the HTTP content.

Gets or sets the HttpContentRangeHeaderValue object that represent the value of an HTTP Content-Range header on the HTTP content.

Gets or sets the HttpMediaTypeHeaderValue object that represent the value of an HTTP Content-Type header on the HTTP content.

Gets or sets the DateTime object that represents the value of an HTTP Expires header on the HTTP content.

Gets or sets the DateTime object that represents the value of an HTTP Last-Modified header on the HTTP content.

Gets the number of objects in the HttpContentHeaderCollection.

Methods summary

Adds a new item to the end of the HttpContentHeaderCollection.

Removes all objects from the HttpContentHeaderCollection.

Retrieves an iterator to the first item in the HttpContentHeaderCollection.

Returns an immutable view of the HttpContentHeaderCollection.

Determines whether the HttpContentHeaderCollection contains the specified key.

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

Finds an item in the HttpContentHeaderCollection if it exists.

Removes a specific object from the HttpContentHeaderCollection.

Returns a string that represents the current HttpContentHeaderCollection object.

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

Constructors

Properties

  • ContentDisposition
    ContentDisposition
    ContentDisposition
    ContentDisposition

    Gets or sets the HttpContentDispositionHeaderValue object that represents the value of an HTTP Content-Disposition header on the HTTP content.

    public HttpContentDispositionHeaderValue ContentDisposition { get; set; }public HttpContentDispositionHeaderValue ContentDisposition { get; set; }Public ReadWrite Property ContentDisposition As HttpContentDispositionHeaderValue

    Property Value

    Remarks

    The following sample code shows a method to get or set the Content-Disposition header value on HTTP content using the ContentDisposition property on the HttpContentHeaderCollection object.

        // Content-Disposition header
        // HttpContentDispositionHeaderValue
        void DemoContentDisposition(IHttpContent content) {
            var h = content.Headers;
    
            HttpContentDispositionHeaderValue value;
            bool ok = HttpContentDispositionHeaderValue.TryParse("attachment; filename=\"myfile.txt\"; myparam=myvalue", out value);
            h.ContentDisposition = value;
    
            h.ContentDisposition = HttpContentDispositionHeaderValue.Parse("attachment; filename=\"myfile.txt\"; myparam=myvalue");
            var header = h.ContentDisposition;
    
            uiLog.Text += "\nCONTENT DISPOSITION HEADER\n";
            // Content-Disposition: attachment; filename="fname.ext"
    
            // ContentDisposition is a HttpContentDispositionHeaderValue and contains:
            // DispositionType, FileName, FileNameStar, Name: all strings
            // Size: nullable long
            // Parameters: IList<HttpNameValueHeaderValue> 
            var parameterString = "";
            foreach (var parameter in header.Parameters) {
                parameterString += string.Format("[{0}={1}] ", parameter.Name, parameter.Value);
            }
            if (parameterString == "") {
                parameterString = "(no parameters)";
            }
    
            uiLog.Text += string.Format("ContentDisposition: DispositionType: {0} FileName: {1} FileNameStar: {2} Name: {3} Parameters: {4} Size: {5} ToString: {6}\n\n", 
            header.DispositionType, header.FileName, header.FileNameStar, header.Name, parameterString, header.Size, header.ToString());
        }
    
  • ContentEncoding
    ContentEncoding
    ContentEncoding
    ContentEncoding

    Gets the HttpContentCodingHeaderValueCollection of HttpContentCodingHeaderValue objects that represent the value of an HTTP Content-Encoding header on the HTTP content.

    public HttpContentCodingHeaderValueCollection ContentEncoding { get; }public HttpContentCodingHeaderValueCollection ContentEncoding { get; }Public ReadOnly Property ContentEncoding As HttpContentCodingHeaderValueCollection

    Property Value

    Remarks

    The following sample code shows a method to get or set the Content-Encoding header value on HTTP content using the ContentEncoding property on the HttpContentHeaderCollection object.

        // Content-Encoding header
        // HttpContentCodingHeaderValueCollection
        // HttpContentCodingHeaderValue [has ContentCoding, a string]
        void DemoContentEncoding(IHttpContent content) {
            var h = content.Headers;
    
            h.ContentEncoding.TryParseAdd("gzip");
            h.ContentEncoding.TryParseAdd("cab, compress");
            h.ContentEncoding.Add(new HttpContentCodingHeaderValue("myencoding"));
    
            var header = h.ContentEncoding;
            uiLog.Text += "\nCONTENT ENCODING HEADER\n";
            foreach (var item in header) {
                uiLog.Text += string.Format("ContentCoding: {0}  ToString: {1}\n", item.ContentCoding, item.ToString());
            }
            uiLog.Text += string.Format("ContentEncoding: ToString: {0}\n\n", header.ToString());
        }
    
  • ContentLanguage
    ContentLanguage
    ContentLanguage
    ContentLanguage

    Gets the HttpLanguageHeaderValueCollection of objects that represent the value of an HTTP Content-Language header on the HTTP content.

    public HttpLanguageHeaderValueCollection ContentLanguage { get; }public HttpLanguageHeaderValueCollection ContentLanguage { get; }Public ReadOnly Property ContentLanguage As HttpLanguageHeaderValueCollection

    Property Value

    Remarks

    The following sample code shows a method to get or set the Content-Language header value on HTTP content using the ContentLanguage property on the HttpContentHeaderCollection object.

        // Content-Language header
        // HttpLanguageHeaderValueCollection (of Windows.Globalization.Language)
        void DemoContentLanguage(IHttpContent content) {
            var h = content.Headers;
    
            h.ContentLanguage.TryParseAdd("en-us");
            h.ContentLanguage.TryParseAdd("ru-ru, ru-us");
            h.ContentLanguage.Add(new Windows.Globalization.Language("ko-ko"));
    
            var header = h.ContentLanguage;
            uiLog.Text += "\nCONTENT LANGUAGE HEADER\n";
            foreach (var item in header) {
                uiLog.Text += string.Format("DisplayName: {0} ToString: {1}\n", item.DisplayName, item.ToString());
            }
            uiLog.Text += string.Format("ContentLanguage: ToString: {0}\n\n", header.ToString());
        }
    
  • ContentLength
    ContentLength
    ContentLength
    ContentLength

    Gets or sets the value of the HTTP Content-Length header on the HTTP content.

    public IReference<ulong> ContentLength { get; set; }public IReference<ulong> ContentLength { get; set; }Public ReadWrite Property ContentLength As IReference<ulong>

    Property Value

    • The value of the HTTP Content-Length header value on the HTTP content. A null value means that the header is absent.

    Remarks

    The following sample code shows a method to get or set the Content-Length header on HTTP content using the ContentLength property on the HttpContentHeaderCollection object.

        // Content-Length header
        // nullable ulong
        void DemoContentLength(IHttpContent content) {
            var h = content.Headers;
    
            h.ContentLength = 313;
    
            var header = h.ContentLength;
            uiLog.Text += "\nCONTENT LENGTH HEADER\n";
    
            uiLog.Text += string.Format("ContentLength: ToString: {0}\n\n", header.ToString());
        }
    
  • ContentLocation
    ContentLocation
    ContentLocation
    ContentLocation

    Gets or sets the value of the HTTP Content-Location header on the HTTP content.

    public Uri ContentLocation { get; set; }public Uri ContentLocation { get; set; }Public ReadWrite Property ContentLocation As Uri

    Property Value

    • The value of the HTTP Content-Location header on the HTTP content. A null value means that the header is absent.

    Remarks

    The following sample code shows a method to get or set the Content-Location header value on HTTP content using the ContentLocation property on the HttpContentHeaderCollection object.

        // Content-Location header
        // Uri
        void DemoContentLocation(IHttpContent content) {
            var h = content.Headers;
    
            h.ContentLocation = new Uri("http://example.com/");
    
            var header = h.ContentLocation;
            uiLog.Text += "\nCONTENT LOCATION HEADER\n";
    
            uiLog.Text += string.Format("ContentLocation: ToString: {0}\n\n", header.ToString());
        }
    
  • ContentMD5
    ContentMD5
    ContentMD5
    ContentMD5

    Gets or sets the value of an HTTP Content-MD5 header on the HTTP content.

    public IBuffer ContentMD5 { get; set; }public IBuffer ContentMD5 { get; set; }Public ReadWrite Property ContentMD5 As IBuffer

    Property Value

    • The value of the HTTP Content-MD5 header on the HTTP content. A null value means that the header is absent.

    Remarks

    The following sample code shows a method to get or set the Content-MD5 header value on HTTP content using the ContentMD5 property on the HttpContentHeaderCollection object.

        // Content-MD5 header
        // IBuffer
        void DemoContentMD5(IHttpContent content) {
            var h = content.Headers;
    
            var str = "This is my content string";
            var alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm("MD5");
            var buff = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary(str, Windows.Security.Cryptography.BinaryStringEncoding.Utf8);
            var hashed = alg.HashData(buff);
            var res = Windows.Security.Cryptography.CryptographicBuffer.EncodeToHexString(hashed);
            h.ContentMD5 = hashed;
    
            var header = h.ContentMD5;
            uiLog.Text += "\nCONTENT MD5 HEADER\n";
    
            uiLog.Text += string.Format("ContentMD5: ToString: {0}\n\n", header.ToString());
            uiLog.Text += string.Format("ContentMD5: base64: {0} hex: {1}\n\n", Convert.ToBase64String(h.ContentMD5.ToArray()), res);
        }
    
  • ContentRange
    ContentRange
    ContentRange
    ContentRange

    Gets or sets the HttpContentRangeHeaderValue object that represent the value of an HTTP Content-Range header on the HTTP content.

    public HttpContentRangeHeaderValue ContentRange { get; set; }public HttpContentRangeHeaderValue ContentRange { get; set; }Public ReadWrite Property ContentRange As HttpContentRangeHeaderValue

    Property Value

    Remarks

    The following sample code shows a method to get or set the Content-Range header value on HTTP content using the ContentRange property on the HttpContentHeaderCollection object.

        // Content-Range header
        // HttpContentRangeHeaderValue (Unit=string, FirstBytePosition, LastBytePosition, Length) all nullable ulong
        //
        void DemoContentRange(IHttpContent content) {
            var h = content.Headers;
            h.ContentRange = new HttpContentRangeHeaderValue (10, 20, 333);
    
            var header = h.ContentRange;
            uiLog.Text += "\nCONTENT RANGE HEADER\n";
    
            uiLog.Text += string.Format("ContentRange: Unit: {0} FirstBytePosition: {1} LastBytePosition: {2} Length: {3} ToString: {4}\n\n", header.Unit, header.FirstBytePosition, header.LastBytePosition, header.Length, header.ToString());
        }
    
  • ContentType
    ContentType
    ContentType
    ContentType

    Gets or sets the HttpMediaTypeHeaderValue object that represent the value of an HTTP Content-Type header on the HTTP content.

    public HttpMediaTypeHeaderValue ContentType { get; set; }public HttpMediaTypeHeaderValue ContentType { get; set; }Public ReadWrite Property ContentType As HttpMediaTypeHeaderValue

    Property Value

    Remarks

    The ContentType property represents the Content-Type header value on HTTP content. The Content-Type header is the MIME type of the content.

    The following sample code shows a method to get or set the Content-Type header value on HTTP content using the ContentType property on the HttpContentHeaderCollection object.

        // Content-Type header
        // HttpMediaTypeHeaderValue (MediaType, Charset are strings, Parameters is an IList<HttpNameValueHeaderValue>)
        //
        void DemoContentType(IHttpContent content) {
            var h = content.Headers;
            h.ContentType = new HttpMediaTypeHeaderValue("myMediaType");
    
            var header = h.ContentType;
            uiLog.Text += "\nCONTENT TYPE HEADER\n";
    
            // Parameters is an IList<HttpNameValueHeaderValue> of Name/Value strings
            var parameterString = "";
            foreach (var parameter in header.Parameters) {
                    parameterString += string.Format("[{0}={1}] ", parameter.Name, parameter.Value);
            }
            if (parameterString == "") {
                    parameterString = "(no parameters)";
            }
    
            uiLog.Text += string.Format("Content-Type: MediaType: {0} CharSet: {1} Parameters: {2} ToString: {3}\n", header.MediaType, header.CharSet, parameterString, header.ToString());
        }
    
  • Expires
    Expires
    Expires
    Expires

    Gets or sets the DateTime object that represents the value of an HTTP Expires header on the HTTP content.

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

    Property Value

    • The object that represents the value of an HTTP Expires header on the HTTP content. A null value means that the header is absent.

    Remarks

    The Expires property represents the value of the Expires header on HTTP content. The Expires header is the date and time after which the HTTP content is considered outdated.

    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 DateTime structure.

    The following sample code shows a method to get or set the Expires header value on HTTP content using the Expires property on the HttpContentHeaderCollection object.

        // Expires header
        // nullable DateTimeOffset
        //
        void DemoExpires(IHttpContent content) {
            var h = content.Headers;
            h.Expires = DateTimeOffset.Now;
    
            var header = h.Expires;
            uiLog.Text += "\nEXPIRES HEADER\n";
            uiLog.Text += String.Format ("Expires: {0}\n", header.ToString());
        }
    
  • LastModified
    LastModified
    LastModified
    LastModified

    Gets or sets the DateTime object that represents the value of an HTTP Last-Modified header on the HTTP content.

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

    Property Value

    • The object that represents the value of an HTTP Last-Modified header on the HTTP content. A null value means that the header is absent.

    Remarks

    The LastModified property represents the Last-Modified header on HTTP content. The Last-Modified header is the date and time that the HTTP content was last modified.

    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 DateTime structure.

    The following sample code shows a method to get or set the Expires header value on HTTP content using the LastModified property on the HttpContentHeaderCollection object.

        // Last-Modified header
        // nullable DateTimeOffset
        //
        void DemoLastModified(IHttpContent content) {
            var h = content.Headers;
            h.LastModified = DateTimeOffset.Now;
    
            var header = h.LastModified;
            uiLog.Text += "\nLAST MODIFIED HEADER\n";
            uiLog.Text += String.Format("LastModified: {0}\n", header.ToString());
        }
    
  • Size
    Size
    Size
    Size

    Gets the number of objects in the HttpContentHeaderCollection.

    public uint Size { get; }public uint Size { get; }Public ReadOnly Property Size As uint

    Property Value

    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.

Methods

  • Append(System.String,System.String)
    Append(System.String,System.String)
    Append(System.String,System.String)
    Append(System.String,System.String)

    Adds a new item to the end of the HttpContentHeaderCollection.

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

    Parameters

    • name
      System.String
      System.String
      System.String
      System.String

      The name of the value to add.

    • value
      System.String
      System.String
      System.String
      System.String

      The item value to add.

  • Clear()
    Clear()
    Clear()
    Clear()

    Removes all objects from the HttpContentHeaderCollection.

    public void Clear()public void Clear()Public Function Clear() As void
  • First()
    First()
    First()
    First()

    Retrieves an iterator to the first item in the HttpContentHeaderCollection.

    public IIterator<IKeyValuePair<string, string>> First()public IIterator<IKeyValuePair<string, string>> First()Public Function First() As IIterator( Of IKeyValuePairstring, string )

    Returns

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

  • GetView()
    GetView()
    GetView()
    GetView()

    Returns an immutable view of the HttpContentHeaderCollection.

    public IMapView<string, string> GetView()public IMapView<string, string> GetView()Public Function GetView() As IMapView( Of string, string )

    Returns

    Remarks

    When programming with .NET, this method is hidden.

  • HasKey(System.String)
    HasKey(System.String)
    HasKey(System.String)
    HasKey(System.String)

    Determines whether the HttpContentHeaderCollection contains the specified key.

    public bool HasKey(System.String key)public bool HasKey(System.String key)Public Function HasKey(key As System.String) As bool

    Parameters

    • key
      System.String
      System.String
      System.String
      System.String

      The key associated with the item to locate.

    Returns

    • bool
      bool
      bool
      bool

      true if the key is found; otherwise, false.

    Remarks

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

  • Insert(System.String,System.String)
    Insert(System.String,System.String)
    Insert(System.String,System.String)
    Insert(System.String,System.String)

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

    public bool Insert(System.String key,System.String value)public bool Insert(System.String key,System.String value)Public Function Insert(key As System.String,value As System.String) As bool

    Parameters

    • key
      System.String
      System.String
      System.String
      System.String

      The key of the item to be inserted.

    • value
      System.String
      System.String
      System.String
      System.String

      The value of the item to insert.

    Returns

    • bool
      bool
      bool
      bool

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

    Remarks

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

  • Lookup(System.String)
    Lookup(System.String)
    Lookup(System.String)
    Lookup(System.String)

    Finds an item in the HttpContentHeaderCollection if it exists.

    public string Lookup(System.String key)public string Lookup(System.String key)Public Function Lookup(key As System.String) As string

    Parameters

    • key
      System.String
      System.String
      System.String
      System.String

      The key of the item to lookup.

    Returns

    • string
      string
      string
      string

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

    Remarks

    Use the HasKey(System.String) method to determine whether the key exists in the HttpContentHeaderCollection.

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

  • Remove(System.String)
    Remove(System.String)
    Remove(System.String)
    Remove(System.String)

    Removes a specific object from the HttpContentHeaderCollection.

    public void Remove(System.String key)public void Remove(System.String key)Public Function Remove(key As System.String) As void

    Parameters

    • key
      System.String
      System.String
      System.String
      System.String

      The key of the item to remove.

  • ToString()
    ToString()
    ToString()
    ToString()

    Returns a string that represents the current HttpContentHeaderCollection object.

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

    Returns

    • string
      string
      string
      string

      A string that represents the current object.

  • TryAppendWithoutValidation(System.String,System.String)
    TryAppendWithoutValidation(System.String,System.String)
    TryAppendWithoutValidation(System.String,System.String)
    TryAppendWithoutValidation(System.String,System.String)

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

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

    Parameters

    • name
      System.String
      System.String
      System.String
      System.String

      The name of the item to append.

    • value
      System.String
      System.String
      System.String
      System.String

      The value of the item to append.

    Returns

    • bool
      bool
      bool
      bool

      true if the item was appended; otherwise false.

    Remarks

    The TryAppendWithoutValidation(System.String,System.String) method is available when you need to work with an HTTP header on HTTP content 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(System.String,System.String) method.

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.ThreadingAttribute
Windows.Foundation.Metadata.ContractVersionAttribute
Windows.Foundation.Metadata.DualApiPartitionAttribute
Windows.Foundation.Metadata.ActivatableAttribute
Windows.Foundation.Metadata.MarshalingBehaviorAttribute

Details

Assembly

Windows.Web.Http.Headers.dll