GetRequestStream Method
Collapse the table of content
Expand the table of content

HttpWebRequest.GetRequestStream Method ()


Gets a Stream object to use to write request data.

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

public override Stream GetRequestStream()

Return Value

Type: System.IO.Stream

A Stream to use to write request data.

Exception Condition

The Method property is GET or HEAD.


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.


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.


The time-out period for the request expired.


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 .2b5e396c-b359-4781-8009-1c8a476c287a

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.


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.


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

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

Snippet section 4 of code snippet {"project_id":"3fedad16-eaf1-41a6-8f96-0c1949c68f32","entity_id":"c6311be3-35b7-428b-9cd3-f2e7bd4bc5aa","entity_type":"CodeSnippet","locale":"en-US"} in source file ({"filename":"/CS/httpwebrequest_contentlength.cs","blob_type":"Source","blob_id":"-002fcs-002fhttpwebrequest-005fcontentlength-002ecs","blob_revision":3}) overlaps with other snippet sections. Ensure the tags are placed correctly.

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft