Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

HttpWebRequest.AddRange Method (Int32, Int32)

Adds a byte range header to the request for a specified range.

Namespace:  System.Net
Assembly:  System (in System.dll)
'Declaration
Public Sub AddRange ( _
	from As Integer, _
	to As Integer _
)
'Usage
Dim instance As HttpWebRequest 
Dim from As Integer 
Dim to As Integer

instance.AddRange(from, to)

Parameters

from
Type: System.Int32

The position at which to start sending data.

to
Type: System.Int32

The position at which to stop sending data.

ExceptionCondition
ArgumentException

rangeSpecifier is invalid.

ArgumentOutOfRangeException

from is greater than to

-or-

from or to is less than 0.

InvalidOperationException

The range header could not be added.

The HttpWebRequest.AddRange method adds a byte range header to the request.

Since all HTTP entities are represented in HTTP messages as sequences of bytes, the concept of a byte range is meaningful for any HTTP entity. However, not all clients and servers need to support byte-range operations.

The Range header on a request allows a client to request that it only wants to receive some part of the specified range of bytes in an HTTP entity. Servers are not required to support Range header requests.

An example of a Range header in an HTTP protocol request that requests the first 100 bytes would be would be the following:

Range: bytes=0-99\r\n\r\n

For this example, the from parameter would be specified as 0 and the to parameter would be specified as 99. The range specifier is automatically set as "bytes" by this method.

A HTTP server indicates support for Range headers with the Accept-Ranges header. An example of the Accept-Ranges header from a server that supports byte-ranges would be as follows:

Accept-Ranges: bytes\r\n\r\n

If an Accept-Ranges header is not received in the header of the response from the server, then the server does not support Range headers. An example of the Accept-Ranges header from a server that does not support ranges, but recognizes the Accept-Ranges header, would be as follows:

Accept-Ranges: none\r\n\r\n

When receiving the response from a range request, only the HTTP headers associated with the entire request are parsed and made available via properties on the HttpWebResponse class. Headers associated with each range are returned in the response.

The following code example adds a range header to the request.

            ' A New 'HttpWebRequest' objetc is created. 
            Dim myHttpWebRequest As HttpWebRequest = WebRequest.Create("http://www.contoso.com")
            myHttpWebRequest.AddRange(50, 150)
            Console.WriteLine("Call AddRange(50, 150)")
			      Console.Write("Resulting Request Headers: ")
			      Console.WriteLine(myHttpWebRequest.Headers.ToString())

            ' The response object of 'HttpWebRequest' is assigned to a 'HttpWebResponse' variable. 
            Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

            ' Displays the headers in the response received
            Console.Write("Resulting Response Headers: ")
			      Console.WriteLine(myHttpWebResponse.Headers.ToString())

            ' Displaying the contents of the page to the console 
            Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
            Dim streamRead As New StreamReader(streamResponse)
            Dim readBuffer(256) As [Char]
            Dim count As Integer = streamRead.Read(readBuffer, 0, 256)
            Console.WriteLine(ControlChars.Cr + "The HTML contents of the page from 50th to 150 charaters are :" + ControlChars.Cr + "  ")
            While count > 0
                Dim outputData As New [String](readBuffer, 0, count)
                Console.WriteLine(outputData)
                count = streamRead.Read(readBuffer, 0, 256)
            End While 
            ' Release the response object resources.
	         streamRead.Close()
	         streamResponse.Close()
            myHttpWebResponse.Close()
            // Create a New 'HttpWebRequest' object.
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)
                (WebRequest.Create("http://www.contoso.com"));
            myHttpWebRequest.AddRange(50, 150);
			      Console.Write("Resulting Headers: ");
			      Console.WriteLine(myHttpWebRequest.get_Headers());

            // Assign the response object of 'HttpWebRequest' to a 
            // 'HttpWebResponse' variable.
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)
                (myHttpWebRequest.GetResponse());

            // Displays the headers in the response received
            Console.Write("Resulting Response Headers: ");
			      Console.WriteLine(myHttpWebResponse.get_Headers());

            // Display the contents of the page to the console.
            Stream streamResponse = myHttpWebResponse.GetResponseStream();
            StreamReader streamRead =  new StreamReader(streamResponse);
            char readBuffer[] = new char[256];
            int count = streamRead.Read(readBuffer, 0, 256);
            Console.WriteLine("\nThe HTML contents of the page from 50th" 
                + " to 150 charaters are :\n  ");
            while(count > 0) {
                String outputData =  new String(readBuffer, 0, count);
                Console.WriteLine(outputData);
                count = streamRead.Read(readBuffer, 0, 256);
            }
            // Release the response object resources.
            streamRead.Close();
            streamResponse.Close();
            myHttpWebResponse.Close();

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.