TransferCodings Class

Represents the Transfer-Encoding header value.

This class is immutable and used to parse and represent the Transfer-Encoding header, which defines how the payload has been transformed.

Fields and Properties

Chunked Property

Gets the Transfer-Encoding value for chunked transfer.
public static TransferCodings Chunked { get; }

GZip Property

Gets the Transfer-Encoding value for GZip compression.
public static TransferCodings GZip { get; }

Identity Property

Gets the Transfer-Encoding value for identity transfer (no encoding).
public static TransferCodings Identity { get; }

Deflate Property

Gets the Transfer-Encoding value for Deflate compression.
public static TransferCodings Deflate { get; }

Methods

Parse(string value) Method

Parses a string value into a TransferCodings object.
public static TransferCodings Parse(string value);
Parameters
value: The string value to parse.

TryParse(string value, out TransferCodings result) Method

Attempts to parse a string value into a TransferCodings object without throwing an exception.
public static bool TryParse(string value, out TransferCodings result);
Parameters
value: The string value to parse.
result: When this method returns, contains the parsed TransferCodings object if the parse was successful, or null otherwise.

Examples

Using TransferCodings

Demonstrates how to use the TransferCodings class to set and parse transfer encoding headers.
using System;
using System.Net.Http.Headers;

public class Example
{
    public static void Main(string[] args)
    {
        // Creating a new HttpRequestMessage
        var request = new HttpRequestMessage(HttpMethod.Post, "https://example.com/upload");

        // Setting the Transfer-Encoding header to Chunked
        request.Headers.TransferEncoding.Add(TransferCodings.Chunked);
        Console.WriteLine($"Request Transfer-Encoding: {request.Headers.TransferEncoding}");

        // Parsing a Transfer-Encoding string
        string encodingString = "gzip, chunked";
        try
        {
            var parsedCodings = TransferCodings.Parse(encodingString);
            Console.WriteLine($"Parsed Transfer-Encoding: {parsedCodings}");

            // You can also check for specific encodings
            if (parsedCodings.Contains(TransferCodings.Chunked))
            {
                Console.WriteLine("The encoding includes Chunked.");
            }
        }
        catch (FormatException)
        {
            Console.WriteLine($"Failed to parse: {encodingString}");
        }

        // Using TryParse
        string validEncoding = "identity";
        TransferCodings result;
        if (TransferCodings.TryParse(validEncoding, out result))
        {
            Console.WriteLine($"TryParse successful for '{validEncoding}': {result}");
        }
        else
        {
            Console.WriteLine($"TryParse failed for '{validEncoding}'.");
        }

        string invalidEncoding = "unknown-encoding";
        if (TransferCodings.TryParse(invalidEncoding, out result))
        {
            Console.WriteLine($"TryParse successful for '{invalidEncoding}': {result}");
        }
        else
        {
            Console.WriteLine($"TryParse failed for '{invalidEncoding}'.");
        }
    }
}