RangeHeader Class

System.Net.Http.Headers

Summary

Represents the value of the Range header in an HTTP request.

This header is used to request a partial response by specifying a byte range or a set of byte ranges for the requested resource.

Inheritance

Constructors

Properties

Methods

Remarks

The RangeHeader class is used to represent the value of the HTTP Range header. This header allows clients to request a portion of a resource, rather than the entire resource. This is commonly used for resuming interrupted downloads or for streaming media.

A typical Range header looks like this:

Range: bytes=0-999

This requests the first 1000 bytes of the resource. The RangeHeader class can parse and represent such values.

The Unit property specifies the unit of the range, commonly bytes. The From and To properties represent the start and end of the byte range. If the end is not specified (e.g., 100-), the To property will be null.

Example

using System.Net.Http.Headers; public class Example { public static void Main() { // Create a RangeHeader for the first 1024 bytes RangeHeader range1 = new RangeHeader(0, 1023); Console.WriteLine("Range 1: " + range1); // Output: bytes=0-1023 // Create a RangeHeader for bytes from 500 onwards RangeHeader range2 = new RangeHeader("bytes", 500, null); Console.WriteLine("Range 2: " + range2); // Output: bytes=500- // Parse a Range Header string string headerString = "bytes=200-399"; RangeHeader parsedRange; if (RangeHeader.TryParse(headerString, out parsedRange)) { Console.WriteLine("Parsed Range Unit: " + parsedRange.Unit); // Output: bytes Console.WriteLine("Parsed Range From: " + parsedRange.From); // Output: 200 Console.WriteLine("Parsed Range To: " + parsedRange.To); // Output: 399 } else { Console.WriteLine("Failed to parse range header."); } } }