Freigeben über


WebResponse.GetResponseStream-Methode

Gibt beim Überschreiben in einer abgeleiteten Klasse den Datenstream von der Internetressource zurück.

Namespace: System.Net
Assembly: System (in system.dll)

Syntax

'Declaration
Public Overridable Function GetResponseStream As Stream
'Usage
Dim instance As WebResponse
Dim returnValue As Stream

returnValue = instance.GetResponseStream
public virtual Stream GetResponseStream ()
public:
virtual Stream^ GetResponseStream ()
public Stream GetResponseStream ()
public function GetResponseStream () : Stream

Rückgabewert

Eine Instanz der Stream-Klasse zum Lesen von Daten aus der Internetressource.

Ausnahmen

Ausnahmetyp Bedingung

NotSupportedException

Es wurde versucht, auf die Methode zuzugreifen, obwohl die Methode in einer abgeleiteten Klasse nicht überschrieben wurde.

Hinweise

Die GetResponseStream-Methode gibt den Datenstream von der Internetressource zurück.

Hinweis

Der Antwortstream muss geschlossen werden, um eine zu hohe Belastung der Systemressourcen zu vermeiden. Der Antwortstream kann durch Aufrufen von Stream.Close oder Close geschlossen werden.

Beispiel

Im folgenden Beispiel wird GetResponseStream verwendet, um eine StreamReader-Instanz zurückzugeben. Mithilfe eines kleinen lokalen Puffers werden die Daten aus dem StreamReader gelesen sowie auf der Konsole ausgegeben.

' Create a 'WebRequest' object with the specified url 
Dim myWebRequest As WebRequest = WebRequest.Create("www.contoso.com")

' Send the 'WebRequest' and wait for response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

' Call method 'GetResponseStream' to obtain stream associated with the response object
Dim ReceiveStream As Stream = myWebResponse.GetResponseStream()

Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")

' Pipe the stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(ReceiveStream, encode)
Console.WriteLine(ControlChars.Cr + "Response stream received")
Dim read(256) As [Char]

' Read 256 charcters at a time    .
Dim count As Integer = readStream.Read(read, 0, 256)
Console.WriteLine("HTML..." + ControlChars.Lf + ControlChars.Cr)
While count > 0

    ' Dump the 256 characters on a string and display the string onto the console.
    Dim str As New [String](read, 0, count)
    Console.Write(str)
    count = readStream.Read(read, 0, 256)

End While
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("https://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( "https://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 = gcnew StreamReader( ReceiveStream,encode );
Console::WriteLine( "\nResponse stream received" );
array<Char>^ read = gcnew array<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 = gcnew 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();

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

WebResponse-Klasse
WebResponse-Member
System.Net-Namespace

Weitere Ressourcen

Verwenden von Streams im Netzwerk