Export (0) Print
Expand All

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)

public void AddRange (
	int from,
	int to
)
public void AddRange (
	int from, 
	int to
)
public function AddRange (
	from : int, 
	to : int
)
Not applicable.

Parameters

from

The position at which to start sending data.

to

The position at which to stop sending data.

Exception typeCondition

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 AddRange method adds a byte range header to the request.

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.

// Create a New 'HttpWebRequest' object .
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
myHttpWebRequest.AddRange(50,150);    
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
// 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();

// Create a New 'HttpWebRequest' object.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)
    (WebRequest.Create("http://www.contoso.com"));
myHttpWebRequest.AddRange(50, 150);
// Assign the response object of 'HttpWebRequest' to a 
// 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)
    (myHttpWebRequest.GetResponse());
// 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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft