TransferCodingParser Class
Namespace: System.Net.Http.Headers
Assembly: System.Net.Http.Formatting.Common
Provides methods for parsing the Transfer-Encoding header.
Remarks
The Transfer-Encoding header can contain a comma-separated list of transfer codings. Each transfer coding must be a valid token. This parser supports the standard transfer codings like "chunked" and "gzip".
Methods
| Name | Description |
|---|---|
| Parse(String) | Parses a string into a TransferCodingParser object. |
| TryParse(String, out TransferCodingParser) | Tries to parse a string into a TransferCodingParser object, returning a boolean value to indicate success. |
Methods
Parse(String)
Method of TransferCodingParser
TransferCodingParser Parse(
String input
);
Parameters
- input: A string representing the value of the Transfer-Encoding header.
Returns
A TransferCodingParser object that represents the parsed transfer coding.
Exceptions
ArgumentNullException: Thrown ifinputis null.FormatException: Thrown ifinputis not a valid transfer coding string.
Example
using System.Net.Http.Headers;
string headerValue = "chunked, gzip";
try
{
TransferCodingParser parser = TransferCodingParser.Parse(headerValue);
Console.WriteLine("Successfully parsed transfer codings.");
// You can now access the parsed transfer codings if the parser
// exposes them (e.g., through a collection property).
// Note: The Parse method itself doesn't directly return the codings,
// but the result is a TransferCodingParser object.
}
catch (FormatException ex)
{
Console.WriteLine($"Error parsing header: {ex.Message}");
}
catch (ArgumentNullException ex)
{
Console.WriteLine($"Input was null: {ex.Message}");
}
TryParse(String, out TransferCodingParser)
Method of TransferCodingParser
Boolean TryParse(
String input,
out TransferCodingParser parser
);
Parameters
- input: A string representing the value of the Transfer-Encoding header.
- parser: When this method returns, contains the TransferCodingParser object if the parsing succeeded, or null if the parsing failed.
Returns
true if the string was parsed successfully; otherwise, false.
Example
using System.Net.Http.Headers;
string headerValue = "deflate";
TransferCodingParser parsedParser;
if (TransferCodingParser.TryParse(headerValue, out parsedParser))
{
Console.WriteLine("Successfully tried to parse transfer codings.");
// parsedParser now holds the TransferCodingParser object.
}
else
{
Console.WriteLine("Failed to parse transfer codings.");
}
string invalidHeaderValue = "invalid-coding";
TransferCodingParser anotherParser;
if (!TransferCodingParser.TryParse(invalidHeaderValue, out anotherParser))
{
Console.WriteLine("Correctly failed to parse invalid transfer coding.");
}