Represents a header value that can be converted to and from a string.
This is a fundamental type for handling HTTP headers within the System.Net.Http namespace, providing a standardized way to parse and format header values.
System.Net.Http.Headers
The HttpLibHeaderValue class serves as an abstract base class for types that represent HTTP header values. It defines the essential contract for parsing header values from strings and formatting them back into strings.
HttpLibHeaderValue must provide logic to parse a string representation of a header value into a structured object.Several concrete classes inherit from HttpLibHeaderValue to handle common HTTP header types:
HttpHeaderValue (A generic base for typed header values)MediaTypeHeaderValue (For Content-Type headers)RangeUnitHeaderValue (For Range headers)TransferCodingHeaderValue (For Transfer-Encoding headers)Subclasses must implement the following members:
public abstract bool TryParse(string value, out HttpLibHeaderValue result);Attempts to parse a string representation of a header value into an instance of a derived class.
value: The string to parse.result: When this method returns, contains the parsed header value if the parsing succeeded, or null if the parsing failed.true if the string was parsed successfully; otherwise, false.public abstract string ToString();Converts the header value object back into its string representation.
Content-Type header:
using System.Net.Http.Headers;
string contentTypeString = "application/json; charset=utf-8";
HttpLibHeaderValue parsedValue;
if (MediaTypeHeaderValue.TryParse(contentTypeString, out parsedValue))
{
MediaTypeHeaderValue mediaType = (MediaTypeHeaderValue)parsedValue;
Console.WriteLine($"Media Type: {mediaType.MediaType}"); // Output: application/json
Console.WriteLine($"Char Set: {mediaType.CharSet}"); // Output: utf-8
Console.WriteLine($"Formatted: {mediaType.ToString()}"); // Output: application/json; charset=utf-8
}
else
{
Console.WriteLine("Failed to parse Content-Type header.");
}
using System.Net.Http.Headers;
var customHeader = new StringWithQualityHeaderValue("gzip");
string headerString = customHeader.ToString();
Console.WriteLine($"Custom Header: {headerString}"); // Output: gzip
HttpLibHeaderValue: The abstract base class.HttpHeaderValue: A generic base for typed header values, offering more robust parsing and validation.MediaTypeHeaderValue: Represents Content-Type and Accept header values.RangeUnitHeaderValue: Represents the unit for Range headers.TransferCodingHeaderValue: Represents values for the Transfer-Encoding header.StringWithQualityHeaderValue: Represents header values that include a quality factor, like Accept-Encoding.HttpHeaderValue<T>A generic abstract base class for typed header values. It simplifies the implementation of TryParse by providing a default mechanism to create new instances.
MediaTypeHeaderValueRepresents a media type header value, such as "text/html; charset=utf-8". Used for Content-Type and Accept headers.
string MediaType: Gets or sets the media type (e.g., "text/html").string CharSet: Gets or sets the character set (e.g., "utf-8").NameValueCollection Parameters: Gets the collection of parameters associated with the media type.static bool TryParse(string value, out MediaTypeHeaderValue parsedValue): Attempts to parse a string into a MediaTypeHeaderValue.RangeUnitHeaderValueRepresents the unit used in a Range header, such as "bytes".
string Unit: Gets the unit name (e.g., "bytes").static bool TryParse(string value, out RangeUnitHeaderValue parsedValue): Attempts to parse a string into a RangeUnitHeaderValue.TransferCodingHeaderValueAbstract base class for transfer coding header values.
TransferCodingWithQualityHeaderValue