This documentation is archived and is not being maintained.

HttpWebRequest.AddRange Method (Int32, Int32)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Sub AddRange( _
   ByVal from As Integer, _
   ByVal to As Integer _
)
[C#]
public void AddRange(
 int from,
 int to
);
[C++]
public: void AddRange(
 int from,
 int to
);
[JScript]
public function AddRange(
   from : int,
 to : int
);

Parameters

from
The position at which to start sending data.
to
The position at which to stop sending data.

Exceptions

Exception Type Condition
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.

Remarks

AddRange adds a byte range header to the request.

Example

[Visual Basic, C#, C++] The following example adds a range header to the request.

[Visual Basic] 
' A New 'HttpWebRequest' objetc is created.
Dim myHttpWebRequest As HttpWebRequest = WebRequest.Create("http://www.contoso.com")
myHttpWebRequest.AddRange(50, 150)
' The response object of 'HttpWebRequest' is assigned to a 'HttpWebResponse' variable.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
' 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()

[C#] 
// 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();

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

HttpWebRequest Class | HttpWebRequest Members | System.Net Namespace | HttpWebRequest.AddRange Overload List

Show: