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.

C#
public override System.IO.Stream GetRequestStream ();

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.

C#
// 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 ();

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

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

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.

C#
public System.IO.Stream GetRequestStream (out System.Net.TransportContext? context);
C#
public System.IO.Stream GetRequestStream (out System.Net.TransportContext context);

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

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1