HttpRequestHeaderCollection.TransferEncoding Property

Definition

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

public:
 property HttpTransferCodingHeaderValueCollection ^ TransferEncoding { HttpTransferCodingHeaderValueCollection ^ get(); };
HttpTransferCodingHeaderValueCollection TransferEncoding();
public HttpTransferCodingHeaderValueCollection TransferEncoding { get; }
var httpTransferCodingHeaderValueCollection = httpRequestHeaderCollection.transferEncoding;
Public ReadOnly Property TransferEncoding As HttpTransferCodingHeaderValueCollection

Property Value

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.

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); for example, "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());
}

Applies to

See also