System.Net.Http.Headers

Classes for HTTP header manipulation.

HttpRangeValue Class

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.

Overview

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.

Constructors

Properties

Methods

Examples

Creating an 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-
            

Parsing an HttpRangeValue


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.
}