WebResponse.GetResponseStream, méthode
Assembly : System (dans system.dll)
La méthode GetResponseStream retourne le flux de données à partir de la ressource Internet.
Remarque |
|---|
| Le flux de réponse doit être fermé pour éviter que les ressources système deviennent insuffisantes. Le flux de réponse peut être fermé en appelant Stream.Close ou Close. |
L'exemple suivant utilise la méthode GetResponseStream pour retourner une instance de StreamReader. Une petite mémoire tampon locale est utilisée pour lire les données à partir de StreamReader et les exporter dans la console.
// Create a 'WebRequest' object with the specified url.
WebRequest myWebRequest = WebRequest.Create("http://www.contoso.com");
// Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse();
// Obtain a 'Stream' object associated with the response object.
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipe the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader( ReceiveStream, encode );
Console.WriteLine("\nResponse stream received");
Char[] read = new Char[256];
// Read 256 charcters at a time.
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
// Dump the 256 characters on a string and display the string onto the console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}
Console.WriteLine("");
// Release the resources of stream object.
readStream.Close();
// Release the resources of response object.
myWebResponse.Close();
// Create a 'WebRequest' object with the specified url.
WebRequest myWebRequest = WebRequest.Create(
"http://www.constoso.com");
// Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse();
// Obtain a 'Stream' object associated with the response object.
Stream receiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipe the stream to a higher level stream reader with the required
// encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
Console.WriteLine("\nResponse stream received");
char read[] = new char[256];
// Read 256 charcters at a time.
int count = readStream.Read(read, 0, 256);
Console.WriteLine("HTML...\r\n");
while (count > 0) {
// Dump the 256 characters on a string and display the string
// onto the console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}
Console.WriteLine("");
// Release the resources of stream object.
readStream.Close();
// Release the resources of response object.
myWebResponse.Close();
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Référence
WebResponse, classeMembres WebResponse
System.Net, espace de noms
Remarque