Freigeben über


HttpWebRequest.GetRequestStream-Methode

Ruft ein Stream-Objekt ab, das zum Schreiben von Anforderungsdaten verwendet werden soll.

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

Syntax

'Declaration
Public Overrides Function GetRequestStream As Stream
'Usage
Dim instance As HttpWebRequest
Dim returnValue As Stream

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

Rückgabewert

Ein Stream, der zum Schreiben von Anforderungsdaten verwendet werden soll.

Ausnahmen

Ausnahmetyp Bedingung

ProtocolViolationException

Die Method-Eigenschaft ist GET oder HEAD.

- oder -

KeepAlive ist true, AllowWriteStreamBuffering ist false, ContentLength ist -1, SendChunked ist false, und Method ist POST oder PUT.

InvalidOperationException

Die GetRequestStream-Methode wird mehrfach aufgerufen.

- oder -

TransferEncoding ist auf einen Wert festgelegt, und SendChunked ist false.

NotSupportedException

Die Cachebestätigung der Anforderung hat angegeben, dass die Antwort für diese Anforderung vom Cache bereitgestellt werden kann. Anforderungen, die Daten schreiben, dürfen jedoch den Cache nicht verwenden. Diese Ausnahme kann auftreten, wenn Sie eine benutzerdefinierte Cachebestätigung verwenden, die nicht ordnungsgemäß implementiert wurde.

WebException

Abort wurde bereits zuvor aufgerufen.

- oder -

Das Timeout für die Anforderung ist abgelaufen.

- oder -

Fehler bei der Verarbeitung der Anforderung.

ObjectDisposedException

In einer .NET Compact Framework-Anwendung wurde ein Anforderungsstream, dessen Inhalt die Länge 0 (null) hat, nicht korrekt abgerufen und geschlossen. Weitere Informationen über das Behandeln von Anforderungen mit einem Inhalt von der Länge 0 (null) finden Sie unter Netzwerkprogrammierung in .NET Compact Framework.

Hinweise

Die GetRequestStream-Methode gibt einen Stream zurück, der zum Senden von Daten für HttpWebRequest verwendet werden soll. Nachdem das Stream-Objekt zurückgegeben wurde, können Sie mit der HttpWebRequest unter Verwendung der Stream.Write-Methode Daten senden.

Vor dem Abrufen des Streams müssen Sie den Wert der ContentLength-Eigenschaft festlegen.

Sie müssen die Stream.Close-Methode aufrufen, um den Stream zu schließen und die Verbindung für die erneute Verwendung freizugeben. Wenn der Stream nicht geschlossen wird, führt dies dazu, dass der Anwendung nicht ausreichend Verbindungen zur Verfügung stehen.

Hinweis

Die Anwendung kann für eine bestimmte Anforderung keine synchronen und asynchronen Methoden kombinieren. Wenn Sie die GetRequestStream-Methode aufrufen, müssen Sie die GetResponse-Methode verwenden, um die Antwort abzurufen.

Hinweis

Dieser Member gibt Ablaufverfolgungsinformationen aus, wenn Sie die Netzwerkablaufverfolgung in der Anwendung aktivieren. Weitere Informationen finden Sie unter Netzwerkablaufverfolgung.

Beispiel

Im folgenden Codebeispiel wird mit der GetRequestStream-Methode eine Streaminstanz zurückgegeben.

' Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest.Method = "POST"

Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the (https://www.contoso.com/codesnippets/next.asp) Uri :")
' Create a new string object to POST data to the Url.
Dim inputData As String = Console.ReadLine()
Dim postData As String = "firstone" + ChrW(61) + inputData
Dim encoding As New ASCIIEncoding()
Dim byte1 As Byte() = encoding.GetBytes(postData)
' Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
' Set the content length of the string being posted.
myHttpWebRequest.ContentLength = byte1.Length
Dim newStream As Stream = myHttpWebRequest.GetRequestStream()
newStream.Write(byte1, 0, byte1.Length)
Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest.ContentLength)
newStream.Close()
// Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest.Method = "POST";
Console.WriteLine ("\nPlease enter the data to be posted to the (https://www.contoso.com/codesnippets/next.asp) Uri :");

// Create a new string object to POST data to the Url.
string inputData = Console.ReadLine ();


string postData = "firstone=" + inputData;
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes (postData);

// Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

// Set the content length of the string being posted.
myHttpWebRequest.ContentLength = byte1.Length;

Stream newStream = myHttpWebRequest.GetRequestStream ();

newStream.Write (byte1, 0, byte1.Length);
Console.WriteLine ("The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest.ContentLength);

// Close the Stream object.
newStream.Close ();
// Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest->Method = "POST";
Console::WriteLine( "\nPlease enter the data to be posted to the (https://www.contoso.com/codesnippets/next.asp) Uri :" );

// Create a new String* Object* to POST data to the Url.
String^ inputData = Console::ReadLine();

String^ postData = String::Concat( "firstone= ", inputData );
ASCIIEncoding^ encoding = gcnew ASCIIEncoding;
array<Byte>^ byte1 = encoding->GetBytes( postData );

// Set the content type of the data being posted.
myHttpWebRequest->ContentType = "application/x-www-form-urlencoded";

// Set the content length of the String* being posted.
myHttpWebRequest->ContentLength = byte1->Length;

Stream^ newStream = myHttpWebRequest->GetRequestStream();

newStream->Write( byte1, 0, byte1->Length );
Console::WriteLine( "The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest->ContentLength );

// Close the Stream Object*.
newStream->Close();
// Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest.set_Method("POST");
Console.WriteLine("\nPlease enter the data to be posted to the "
    + "(https://www.contoso.com/codesnippets/next.asp) Uri :");

// Create a new string object to POST data to the Url.
String inputData = Console.ReadLine();

String postData = "firstone=" + inputData;
ASCIIEncoding encoding = new ASCIIEncoding();
ubyte byte1[] = encoding.GetBytes(postData);

// Set the content type of the data being posted.
myHttpWebRequest.set_ContentType("application/x-www-form-"
    +"urlencoded");
// Set the content length of the string being posted.
myHttpWebRequest.set_ContentLength(postData.length());

Stream newStream = myHttpWebRequest.GetRequestStream();

newStream.Write(byte1, 0, byte1.length);
Console.WriteLine("The value of 'ContentLength' property after"
    + " sending the data is {0}", 
    System.Convert.ToString(myHttpWebRequest.get_ContentLength()));
// Close the Stream object.
newStream.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

HttpWebRequest-Klasse
HttpWebRequest-Member
System.Net-Namespace

Weitere Ressourcen

defaultProxy-Element (Netzwerkeinstellungen)