Windows.Web.Http.Headers.HttpContentHeaderCollection
December 6, 2021 ยท View on GitHub
-description
Provides a collection of the HTTP headers associated with content on an HTTP request or response.
-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());
}
-see-also
HttpBufferContent, HttpFormUrlEncodedContent, HttpMultipartContent, HttpMultipartFormDataContent, HttpRequestMessage, HttpResponseMessage, HttpStreamContent, HttpStringContent, IMap(String, String), IIterable(IKeyValuePair), IStringable