Classes for HTTP header manipulation.
Represents the value of an HTTP Range header. The Range header is used to indicate which part of a resource the client wants to fetch. This class provides properties to access the range start and end points, as well as the unit of the range.
The HttpRangeValue class is part of the
System.Net.Http.Headers namespace and is designed to
parse and represent the content of the HTTP Range header. This header allows clients
to request specific portions of a resource, which is particularly useful for resuming
file downloads or for fetching specific parts of large media files.
A typical Range header looks like: Range: bytes=0-499. This class allows you to
programmatically work with such values.
Initializes a new instance of the HttpRangeValue class with the specified byte range.
Initializes a new instance of the HttpRangeValue class with the specified start byte and an open-ended range.
Gets the start of the range.
Gets the end of the range. If this property is null, the range is open-ended.
Gets the unit of the range (e.g., "bytes").
Parses a string representation of a Range header value and returns an instance of HttpRangeValue.
Tries to parse a string representation of a Range header value. Returns true if parsing succeeds, false otherwise.
Returns a string representation of the HttpRangeValue.
using System.Net.Http.Headers;
// Create a range from byte 0 to byte 499
HttpRangeValue range1 = new HttpRangeValue(0, 499);
Console.WriteLine(range1.ToString()); // Output: bytes=0-499
// Create an open-ended range from byte 1000 onwards
HttpRangeValue range2 = new HttpRangeValue(1000);
Console.WriteLine(range2.ToString()); // Output: bytes=1000-
using System.Net.Http.Headers;
string rangeString = "bytes=500-1000";
HttpRangeValue parsedRange;
if (HttpRangeValue.TryParse(rangeString, out parsedRange))
{
Console.WriteLine($"Parsed From: {parsedRange.From}, To: {parsedRange.To}, Unit: {parsedRange.Unit}");
// Output: Parsed From: 500, To: 1000, Unit: bytes
}
string invalidRangeString = "invalid-range";
if (!HttpRangeValue.TryParse(invalidRangeString, out parsedRange))
{
Console.WriteLine("Failed to parse invalid range string.");
// Output: Failed to parse invalid range string.
}