Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.
Traduction
Anglais

HttpWebRequest.AddRange méthode (Int32, Int32)

 

Date de publication : novembre 2016

Ajoute un en-tête de plage d'octets à la requête d'une plage spécifiée.

Espace de noms:   System.Net
Assembly:  System (dans System.dll)

public void AddRange(
	int from,
	int to
)

Paramètres

from
Type: System.Int32

Position à laquelle démarrer l'envoi de données.

to
Type: System.Int32

Position à laquelle arrêter l'envoi de données.

Exception Condition
ArgumentException

rangeSpecifier n'est pas valide.

ArgumentOutOfRangeException

from est supérieur à to

ou

from ou to est inférieur à 0.

InvalidOperationException

Impossible d’ajouter l’en-tête de plage.

The M:System.Net.HttpWebRequest.AddRange(System.Int32,System.Int32) 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 T:System.Net.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);	
Console.WriteLine("Call AddRange(50,150)");
Console.Write("Resulting Request Headers: ");
Console.WriteLine(myHttpWebRequest.Headers.ToString());

// 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.Headers.ToString());

// 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 characters 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();

.NET Framework
Disponible depuis 1.1
Retour au début
Afficher: