HttpWebResponse.GetResponseStream-Methode
Ruft den Stream ab, der zum Lesen des Textkörpers der Serverantwort verwendet wird.
Namespace: System.Net
Assembly: System (in system.dll)
Syntax
'Declaration
Public Overrides Function GetResponseStream As Stream
'Usage
Dim instance As HttpWebResponse
Dim returnValue As Stream
returnValue = instance.GetResponseStream
public override Stream GetResponseStream ()
public:
virtual Stream^ GetResponseStream () override
public Stream GetResponseStream ()
public override function GetResponseStream () : Stream
Rückgabewert
Ein Stream mit dem Textkörper der Antwort.
Ausnahmen
Ausnahmetyp | Bedingung |
---|---|
Es ist kein Antwortstream vorhanden. |
|
Die aktuelle Instanz wurde bereits verworfen. |
Hinweise
Die GetResponseStream-Methode gibt den Datenstream von der angeforderten Internetressource zurück.
Hinweis
Sie müssen entweder die Stream.Close-Methode oder die HttpWebResponse.Close-Methode aufrufen, um den Stream zu schließen und die Verbindung für die erneute Verwendung freizugeben. Es ist nicht erforderlich, sowohl die Stream.Close-Methode als auch die HttpWebResponse.Close-Methode aufzurufen. Es tritt aber auch kein Fehler auf, wenn Sie beide Methoden aufrufen. Wenn der Stream nicht geschlossen wird, führt dies dazu, dass der Anwendung nicht ausreichend Verbindungen zur Verfügung stehen.
Hinweis
Dieser Member gibt Ablaufverfolgungsinformationen aus, wenn Sie die Netzwerkablaufverfolgung in der Anwendung aktivieren. Weitere Informationen finden Sie unter Netzwerkablaufverfolgung.
Beispiel
Im folgenden Beispiel wird veranschaulicht, wie mithilfe von GetResponseStream die Stream-Instanz zurückgegeben wird, mit der die Antwort vom Server gelesen wird.
' Creates an HttpWebRequest for the specified URL.
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
' Sends the request and waits for a response.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
' Calls the method GetResponseStream to return the stream associated with the response.
Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream()
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
' Pipes the response stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(receiveStream, encode)
Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "Response stream received")
Dim read(256) As [Char]
' Reads 256 characters at a time.
Dim count As Integer = readStream.Read(read, 0, 256)
Console.WriteLine("HTML..." + ControlChars.Lf + ControlChars.Cr)
While count > 0
' Dumps the 256 characters to a string and displays the string to the console.
Dim str As New [String](read, 0, count)
Console.Write(str)
count = readStream.Read(read, 0, 256)
End While
Console.WriteLine("")
' Releases the resources of the Stream.
readStream.Close()
' Releases the resources of the response.
myHttpWebResponse.Close()
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader( receiveStream, encode );
Console.WriteLine("\r\nResponse stream received.");
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}
Console.WriteLine("");
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( url ) );
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
// Gets the stream associated with the response.
Stream^ receiveStream = myHttpWebResponse->GetResponseStream();
Encoding^ encode = System::Text::Encoding::GetEncoding( "utf-8" );
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader^ readStream = gcnew StreamReader( receiveStream,encode );
Console::WriteLine( "\r\nResponse stream received." );
array<Char>^ read = gcnew array<Char>(256);
// Reads 256 characters at a time.
int count = readStream->Read( read, 0, 256 );
Console::WriteLine( "HTML...\r\n" );
while ( count > 0 )
{
// Dumps the 256 characters on a String* and displays the String* to the console.
String^ str = gcnew String( read,0,count );
Console::Write( str );
count = readStream->Read( read, 0, 256 );
}
Console::WriteLine( "" );
// Releases the resources of the response.
myHttpWebResponse->Close();
// Releases the resources of the Stream.
readStream->Close();
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)
WebRequest.Create(url);
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)
myHttpWebRequest.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the
// required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
Console.WriteLine("\r\nResponse stream received.");
char read[] = new char[256];
// Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);
Console.WriteLine("HTML...\r\n");
while (count > 0) {
// Dumps the 256 characters on a string and displays the
// string to the console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}
Console.WriteLine("");
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.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
HttpWebResponse-Klasse
HttpWebResponse-Member
System.Net-Namespace