System.Net.Http.Headers.TransferEncodingProductParser

Represents a parser for the Transfer-Encoding header value.

Namespace

System.Net.Http.Headers

Assembly

System.Net.Http.dll

Syntax

public static class TransferEncodingProductParser

Remarks

The Transfer-Encoding header is used by HTTP servers to indicate that the payload has been transformed by a hop-by-hop data transfer mechanism. This header is not usually set by client applications, but rather by HTTP agents (such as web servers and proxy servers) to ensure that the payload is correctly interpreted by the recipient.

The TransferEncodingProductParser class provides static methods to parse the value of the Transfer-Encoding header. The header value is a comma-separated list of transfer coding names. Each transfer coding name can be optionally followed by parameters. The parser handles the correct interpretation of these components.

Methods

Parse(string)

Parses a transfer encoding product from a string.

Syntax

public static TransferCodingWithQualityHeaderValue Parse(string input);

Parameters

Returns

A TransferCodingWithQualityHeaderValue object representing the parsed transfer coding.

Exceptions

TryParse(string, out TransferCodingWithQualityHeaderValue)

Tries to parse a transfer encoding product from a string.

Syntax

public static bool TryParse(string input, out TransferCodingWithQualityHeaderValue result);

Parameters

Returns

true if the string was parsed successfully; otherwise, false.

Example

The following example shows how to parse a Transfer-Encoding header using TransferEncodingProductParser.Parse.

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

public class Example
{
    public static void Main()
    {
        string headerValue = "chunked, gzip;q=1.0, identity;q=0.5";

        try
        {
            TransferCodingHeaderValue parsedHeader = TransferCodingHeaderValue.ParseList(headerValue);

            Console.WriteLine("Parsed Transfer-Encoding Header:");
            foreach (TransferCodingWithQualityHeaderValue coding in parsedHeader)
            {
                Console.WriteLine($"- Name: {coding.Value}");
                Console.WriteLine($"  Quality: {coding.Quality ?? 1.0}");
                if (coding.Parameters.Count > 0)
                {
                    Console.WriteLine("  Parameters:");
                    foreach (NameValueHeaderValue parameter in coding.Parameters)
                    {
                        Console.WriteLine($"  - {parameter.Name}: {parameter.Value}");
                    }
                }
            }
        }
        catch (FormatException ex)
        {
            Console.WriteLine($"Error parsing header: {ex.Message}");
        }
    }
}