Dela via


HttpContentHeaderCollection Class

Definition

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

public ref class HttpContentHeaderCollection sealed : IIterable<IKeyValuePair<Platform::String ^, Platform::String ^> ^>, IMap<Platform::String ^, Platform::String ^>, IStringable
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class HttpContentHeaderCollection final : IIterable<IKeyValuePair<winrt::hstring, winrt::hstring const&>>, IMap<winrt::hstring, winrt::hstring const&>, IStringable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class HttpContentHeaderCollection final : IIterable<IKeyValuePair<winrt::hstring, winrt::hstring const&>>, IMap<winrt::hstring, winrt::hstring const&>, IStringable
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class HttpContentHeaderCollection : IDictionary<string,string>, IEnumerable<KeyValuePair<string,string>>, IStringable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class HttpContentHeaderCollection : IDictionary<string,string>, IEnumerable<KeyValuePair<string,string>>, IStringable
function HttpContentHeaderCollection()
Public NotInheritable Class HttpContentHeaderCollection
Implements IDictionary(Of String, String), IEnumerable(Of KeyValuePair(Of String, String)), IStringable
Inheritance
Object Platform::Object IInspectable HttpContentHeaderCollection
Attributes
Implements
IMap<String,String> IDictionary<String,String> IMap<Platform::String,Platform::String> IMap<winrt::hstring,winrt::hstring> IIterable<IKeyValuePair<K,V>> IEnumerable<KeyValuePair<K,V>> IIterable<IKeyValuePair<String,String>> IEnumerable<KeyValuePair<String,String>> IIterable<IKeyValuePair<Platform::String,Platform::String>> IIterable<IKeyValuePair<winrt::hstring,winrt::hstring>> IStringable

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

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

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.

Constructors

HttpContentHeaderCollection()

Initializes a new instance of the HttpContentHeaderCollection class.

Properties

ContentDisposition

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

ContentEncoding

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

ContentLanguage

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

ContentLength

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

ContentLocation

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

ContentMD5

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

ContentRange

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

ContentType

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

Expires

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

LastModified

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

Size

Gets the number of objects in the HttpContentHeaderCollection.

Methods

Append(String, String)

Adds a new item to the end of the HttpContentHeaderCollection.

Clear()

Removes all objects from the HttpContentHeaderCollection.

First()

Retrieves an iterator to the first item in the HttpContentHeaderCollection.

GetView()

Returns an immutable view of the HttpContentHeaderCollection.

HasKey(String)

Determines whether the HttpContentHeaderCollection contains the specified key.

Insert(String, String)

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

Lookup(String)

Finds an item in the HttpContentHeaderCollection if it exists.

Remove(String)

Removes a specific object from the HttpContentHeaderCollection.

ToString()

Returns a string that represents the current HttpContentHeaderCollection object.

TryAppendWithoutValidation(String, String)

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

Applies to

See also