WarningHeaderValueSegment

System.Net.Http.Headers
Represents a single segment within a `Warning` header value. The `Warning` header is used to provide additional information about the response, typically related to caching or potential issues with the origin server.

Syntax

public sealed class WarningHeaderValueSegment

Summary

The `WarningHeaderValueSegment` class is a fundamental component for parsing and constructing `Warning` headers according to RFC 7234. Each segment typically contains a warning code, the agent acting on behalf of the origin server, and a text description.

Constructors

WarningHeaderValueSegment(string agent, string text, int code)

Initializes a new instance of the WarningHeaderValueSegment class with the specified warning code, agent, and text.
public WarningHeaderValueSegment(string agent, string text, int code);

Parameters:

agent: The agent acting on behalf of the origin server. Typically a hostname or email address.

text: A human-readable description of the warning.

code: The warning code, which is an integer.

WarningHeaderValueSegment(string segment)

Initializes a new instance of the WarningHeaderValueSegment class by parsing a string representation of a warning segment.
public WarningHeaderValueSegment(string segment);

Parameters:

segment: A string representing the warning header segment, e.g., "199 blackcat.example.com "This is a warning."".

This constructor is useful for parsing incoming `Warning` headers. It will throw an FormatException if the provided string is not a valid warning segment.

Properties

Agent

Gets the agent that issued the warning.
public string Agent { get; }

Code

Gets the warning code.
public int Code { get; }

Text

Gets the human-readable text description of the warning.
public string Text { get; }

Methods

ToString()

Converts the WarningHeaderValueSegment to its string representation.
public override string ToString();

Returns:

A string representing the warning header segment in the format "code agent text".

Parse(string value)

Parses a string representation of a `Warning` header value and returns a collection of `WarningHeaderValueSegment` objects.
public static System.Collections.Generic.IList<WarningHeaderValueSegment> Parse(string value);

Parameters:

value: The string representation of the `Warning` header value.

Returns:

An IList<WarningHeaderValueSegment> containing the parsed segments.

This static method is a convenient way to parse a complete `Warning` header string, which may contain multiple segments separated by commas.

TryParse(string value, out WarningHeaderValueSegment segment)

Tries to parse a string representation of a `Warning` header value into a single `WarningHeaderValueSegment`.
public static bool TryParse(string value, [System.Runtime.InteropServices.OutAttribute] [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute](false)] out WarningHeaderValueSegment segment);

Parameters:

value: The string to parse.

segment: When this method returns, contains the parsed `WarningHeaderValueSegment` if the parse succeeded, or null if it failed.

Returns:

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

This method is similar to `Parse`, but it returns a boolean indicating success or failure instead of throwing an exception.

Remarks

The `Warning` header is defined in RFC 7234, Section 5.5. It's often used by intermediate caches or proxies to indicate that a transformation or modification has occurred to the response, or that there might be an issue with the origin server's content. The WarningHeaderValueSegment class encapsulates one of these individual warning notifications.

Common warning codes include: When constructing or parsing `Warning` headers, ensure that the `agent` and `text` are properly formatted and quoted if they contain spaces or special characters, although the class handles much of this automatically.

See Also