Edit

Share via


HttpWebRequest.GetRequestStream Method

Definition

Gets a Stream object to use to write request data.

Overloads

GetRequestStream()

Gets a Stream object to use to write request data.

GetRequestStream(TransportContext)

Gets a Stream object to use to write request data and outputs the TransportContext associated with the stream.

GetRequestStream()

Source:
HttpWebRequest.cs
Source:
HttpWebRequest.cs
Source:
HttpWebRequest.cs

Gets a Stream object to use to write request data.

public:
 override System::IO::Stream ^ GetRequestStream();
public override System.IO.Stream GetRequestStream ();
override this.GetRequestStream : unit -> System.IO.Stream
Public Overrides Function GetRequestStream () As Stream

Returns

A Stream to use to write request data.

Exceptions

The Method property is GET or HEAD.

-or-

KeepAlive is true, AllowWriteStreamBuffering is false, ContentLength is -1, SendChunked is false, and Method is POST or PUT.

The GetRequestStream() method is called more than once.

-or-

TransferEncoding is set to a value and SendChunked is false.

The request cache validator indicated that the response for this request can be served from the cache; however, requests that write data must not use the cache. This exception can occur if you are using a custom cache validator that is incorrectly implemented.

Abort() was previously called.

-or-

The time-out period for the request expired.

-or-

An error occurred while processing the request.

In a .NET Compact Framework application, a request stream with zero content length was not obtained and closed correctly. For more information about handling zero content length requests, see Network Programming in the .NET Compact Framework.

Examples

The following code example uses the GetRequestStream method to return a stream instance.

// Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest->Method = "POST";
Console::WriteLine( "\nPlease enter the data to be posted to the (http://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.Method = "POST";
Console.WriteLine ("\nPlease enter the data to be posted to the (http://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(ControlChars.Cr + "Please enter the data to be posted to the (http://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()

Remarks

Caution

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

The GetRequestStream method returns a stream to use to send data for the HttpWebRequest. After the Stream object has been returned, you can send data with the HttpWebRequest by using the Stream.Write method.

If an application needs to set the value of the ContentLength property, then this must be done before retrieving the stream.

You must call the Stream.Close method to close the stream and release the connection for reuse. Failure to close the stream causes your application to run out of connections.

Note

Your application cannot mix synchronous and asynchronous methods for a particular request. If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

See also

Applies to

GetRequestStream(TransportContext)

Source:
HttpWebRequest.cs
Source:
HttpWebRequest.cs
Source:
HttpWebRequest.cs

Gets a Stream object to use to write request data and outputs the TransportContext associated with the stream.

public:
 System::IO::Stream ^ GetRequestStream([Runtime::InteropServices::Out] System::Net::TransportContext ^ % context);
public System.IO.Stream GetRequestStream (out System.Net.TransportContext? context);
public System.IO.Stream GetRequestStream (out System.Net.TransportContext context);
override this.GetRequestStream : TransportContext -> System.IO.Stream
Public Function GetRequestStream (ByRef context As TransportContext) As Stream

Parameters

Returns

A Stream to use to write request data.

Exceptions

The GetRequestStream() method was unable to obtain the Stream.

The GetRequestStream() method is called more than once.

-or-

TransferEncoding is set to a value and SendChunked is false.

The request cache validator indicated that the response for this request can be served from the cache; however, requests that write data must not use the cache. This exception can occur if you are using a custom cache validator that is incorrectly implemented.

The Method property is GET or HEAD.

-or-

KeepAlive is true, AllowWriteStreamBuffering is false, ContentLength is -1, SendChunked is false, and Method is POST or PUT.

Abort() was previously called.

-or-

The time-out period for the request expired.

-or-

An error occurred while processing the request.

Remarks

Caution

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

The GetRequestStream method returns a stream to use to send data for the HttpWebRequest and outputs the TransportContext associated with the stream. After the Stream object has been returned, you can send data with the HttpWebRequest by using the Stream.Write method.

Some applications that use integrated Windows authentication with extended protection may need to be able to query the transport layer used by HttpWebRequest in order to retrieve the channel binding token (CBT) from the underlying TLS channel. The GetRequestStream method provides access to this information for HTTP methods which have a request body (POST and PUT requests). This is only needed if the application is implementing its own authentication and needs access to the CBT.

If an application needs to set the value of the ContentLength property, then this must be done before retrieving the stream.

You must call the Stream.Close method to close the stream and release the connection for reuse. Failure to close the stream causes your application to run out of connections.

Note

Your application cannot mix synchronous and asynchronous methods for a particular request. If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

See also

Applies to