Represents a Retry-After header value.
Namespace: System.Net.Http.Headers
Assembly: System.Net.Http.dll
public struct RetryAfterHeaderValue
Initializes a new instance of the RetryAfterHeaderValue struct with a specified delay as a DateTimeOffset.
public RetryAfterHeaderValue(DateTimeOffset delay)
Initializes a new instance of the RetryAfterHeaderValue struct with a specified delay as a TimeSpan.
public RetryAfterHeaderValue(TimeSpan delay)
Gets the time interval to delay before retrying the request.
public TimeSpan Delta { get; }
Gets the date and time to retry the request.
public DateTimeOffset? Date { get; }
Gets the internal representation of the delay in seconds.
internal int InternalDelta { get; }
Determines whether the specified object is equal to the current object.
public override bool Equals(object obj)
Determines whether the specified RetryAfterHeaderValue is equal to the current object.
public bool Equals(RetryAfterHeaderValue other)
Serves as the default hash function.
public override int GetHashCode()
Parses a Retry-After header value.
public static RetryAfterHeaderValue Parse(string value)
A RetryAfterHeaderValue instance.
Returns a string representation of the RetryAfterHeaderValue object.
public override string ToString()
Tries to parse a Retry-After header value.
public static bool TryParse(string value, out RetryAfterHeaderValue result)
RetryAfterHeaderValue instance that was parsed.true if the string was parsed successfully; otherwise, false.
using System;
using System.Net.Http.Headers;
// ...
// Create a RetryAfterHeaderValue with a delay of 30 seconds
RetryAfterHeaderValue retryDelay = new RetryAfterHeaderValue(TimeSpan.FromSeconds(30));
Console.WriteLine($"Retry-After: {retryDelay}"); // Output: Retry-After: 30
// Parse a string value
string headerValue = "60";
if (RetryAfterHeaderValue.TryParse(headerValue, out RetryAfterHeaderValue parsedDelay))
{
Console.WriteLine($"Parsed retry delay: {parsedDelay.Delta.TotalSeconds} seconds");
}
using System;
using System.Net.Http.Headers;
// ...
// Create a RetryAfterHeaderValue with a specific date and time
DateTimeOffset retryDate = new DateTimeOffset(2023, 12, 31, 23, 59, 59, TimeSpan.Zero);
RetryAfterHeaderValue retryDateTime = new RetryAfterHeaderValue(retryDate);
Console.WriteLine($"Retry-After: {retryDateTime}"); // Output: Retry-After: Sun, 31 Dec 2023 23:59:59 GMT
// Parse a string value with a date
string dateHeaderValue = "Tue, 01 Jan 2024 12:00:00 GMT";
if (RetryAfterHeaderValue.TryParse(dateHeaderValue, out RetryAfterHeaderValue parsedDate))
{
Console.WriteLine($"Parsed retry date: {parsedDate.Date}");
}
The Retry-After header indicates how long the user agent should wait before making another request.
It can be specified as a number of seconds (a TimeSpan) or as a specific date and time (a DateTimeOffset).
When a server responds with a 503 (Service Unavailable) or 429 (Too Many Requests) status code, it may include a Retry-After header to suggest a delay.