.NET Library / System.Net.Http / Headers / RetryAfterHeaderValue

RetryAfterHeaderValue Struct

Represents a Retry-After header value.

Namespace: System.Net.Http.Headers

Assembly: System.Net.Http.dll

Syntax


public struct RetryAfterHeaderValue
        

Constructors

RetryAfterHeaderValue(DateTimeOffset delay)

Initializes a new instance of the RetryAfterHeaderValue struct with a specified delay as a DateTimeOffset.


public RetryAfterHeaderValue(DateTimeOffset delay)
            

Parameters

RetryAfterHeaderValue(TimeSpan delay)

Initializes a new instance of the RetryAfterHeaderValue struct with a specified delay as a TimeSpan.


public RetryAfterHeaderValue(TimeSpan delay)
            

Parameters

Properties

Delta { get; }

Gets the time interval to delay before retrying the request.


public TimeSpan Delta { get; }
            

Date { get; }

Gets the date and time to retry the request.


public DateTimeOffset? Date { get; }
            

InternalDelta { get; }

Gets the internal representation of the delay in seconds.


internal int InternalDelta { get; }
            

Methods

Equals(object obj)

Determines whether the specified object is equal to the current object.


public override bool Equals(object obj)
            

Equals(RetryAfterHeaderValue other)

Determines whether the specified RetryAfterHeaderValue is equal to the current object.


public bool Equals(RetryAfterHeaderValue other)
            

GetHashCode()

Serves as the default hash function.


public override int GetHashCode()
            

Parse(string value)

Parses a Retry-After header value.


public static RetryAfterHeaderValue Parse(string value)
            

Parameters

Returns

A RetryAfterHeaderValue instance.

ToString()

Returns a string representation of the RetryAfterHeaderValue object.


public override string ToString()
            

TryParse(string value, out RetryAfterHeaderValue result)

Tries to parse a Retry-After header value.


public static bool TryParse(string value, out RetryAfterHeaderValue result)
            

Parameters

Returns

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

Examples

Using TimeSpan


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 DateTimeOffset


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}");
}
            

Remarks

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.