RetryConditionHeaderParser Class
public sealed class RetryConditionHeaderParser : HttpHeadersParser<RetryConditionHeaderValue>
Summary
Parses a header value into a RetryConditionHeaderValue object.
This class is responsible for converting string representations of retry conditions (e.g., `Date`, `Retry-After`) into strongly-typed objects that can be easily used by HTTP clients and servers.
Description
The RetryConditionHeaderParser class implements the HttpHeadersParser generic interface to provide specific parsing logic for retry condition headers. These headers are used in HTTP responses to indicate when a client should retry a request that has failed or been temporarily rejected.
Common retry condition headers include:
Retry-After: Specifies the amount of time (in seconds) or a specific date after which the client should retry the request.Date: The date and time at which the response was generated. This can be used in conjunction withRetry-Afterif it specifies a date.
This parser helps in creating robust HTTP communication by ensuring that retry directives are interpreted correctly.
Public Constructors
-
RetryConditionHeaderParser()
Initializes a new instance of the
RetryConditionHeaderParserclass.
Public Methods
-
RetryConditionHeaderValue Parse(string input)
Parses the specified header value.
Parameters
input: The string to parse.Returns
A
RetryConditionHeaderValueobject representing the parsed header value.Exceptions
FormatException: If the input string is not a valid retry condition header value. -
bool TryParse(string input, out RetryConditionHeaderValue parsedValue)
Attempts to parse the specified header value without throwing an exception.
Parameters
input: The string to parse.parsedValue: When this method returns, contains the parsedRetryConditionHeaderValueobject if parsing was successful, ornullotherwise. This parameter is passed uninitialized.Returns
trueif the string was parsed successfully; otherwise,false.
Inherited Members
This class inherits members from HttpHeadersParser<RetryConditionHeaderValue>.
- static RetryConditionHeaderValue Parse(string input) - From
HttpHeadersParser<T> - static bool TryParse(string input, out RetryConditionHeaderValue parsedValue) - From
HttpHeadersParser<T>
Example
using System;
using System.Net.Http.Headers;
public class Example
{
public static void Main(string[] args)
{
string dateHeader = "Retry-After: Tue, 15 Nov 1994 12:45:26 GMT";
string secondsHeader = "Retry-After: 60";
try
{
// Parse a date-based Retry-After header
RetryConditionHeaderValue retryAfterDate = RetryConditionHeaderValue.Parse(dateHeader.Split(':')[1].Trim());
Console.WriteLine($"Parsed date: {retryAfterDate.Date}");
// Parse a seconds-based Retry-After header
RetryConditionHeaderValue retryAfterSeconds = RetryConditionHeaderValue.Parse(secondsHeader.Split(':')[1].Trim());
Console.WriteLine($"Parsed seconds: {retryAfterSeconds.Delta}");
}
catch (FormatException ex)
{
Console.WriteLine($"Error parsing header: {ex.Message}");
}
}
}