.NET API Reference

TransferCodings Class

Represents the Transfer-Encoding HTTP header. This class is part of the System.Net.Http.Headers namespace.

Summary

The TransferCodings class provides a strongly-typed representation of the Transfer-Encoding header value used in HTTP requests and responses. This header specifies the transfer encoding methods that are applied to the message. Common transfer codings include chunked. This class is often used in conjunction with the HttpRequestHeaders and HttpResponseHeaders collections.

Remarks

The Transfer-Encoding header is a hop-by-hop header that is used to indicate which transfer-mechanism has been applied to the message body. It is an optional header. When present, it must be one or more of the following:

The TransferCodings class provides methods to parse and serialize the Transfer-Encoding header value.

Fields

This class does not expose any public fields.

Properties

This class does not expose any public properties.

Methods

The TransferCodings class typically exposes static methods for parsing and formatting the Transfer-Encoding header value. For instance, methods to get or set the TransferCodings object from an HttpHeaders collection.

Static Methods

public static TransferCodingHeaderValue Parse(string input);

Parses a string representation of a Transfer-Encoding header value and returns an instance of TransferCodingHeaderValue.

public static bool TryParse(string input, out TransferCodingHeaderValue parsedValue);

Attempts to parse a string representation of a Transfer-Encoding header value and returns a value that indicates whether the operation succeeded.

Instance Methods

While TransferCodings itself is often treated as a value type or a container for parsing/formatting, individual transfer codings might be represented by other classes like TransferCodingHeaderValue, which would have instance methods.

public override string ToString();

Returns a string representation of the current TransferCodings object.

Examples

Parsing a Transfer-Encoding Header


using System;
using System.Net.Http.Headers;

// ...

string headerValue = "chunked, gzip";
TransferCodingHeaderValue transferCoding = TransferCodingHeaderValue.Parse(headerValue);

Console.WriteLine($"Parsed Transfer-Encoding: {transferCoding}");
// Output: Parsed Transfer-Encoding: chunked, gzip
        

Adding Transfer-Encoding to HttpRequestHeaders


using System;
using System.Net.Http;
using System.Net.Http.Headers;

// ...

var request = new HttpRequestMessage(HttpMethod.Get, "http://example.com");

// Add chunked encoding
request.Headers.TransferEncoding.Add(new TransferCodingHeaderValue("chunked"));

// Add gzip encoding (if supported by the client/server negotiation)
request.Headers.TransferEncoding.Add(new TransferCodingHeaderValue("gzip"));

Console.WriteLine($"Request Transfer-Encoding: {request.Headers.TransferEncoding}");
// Output: Request Transfer-Encoding: chunked, gzip
        

See Also