Export (0) Print
Expand All
2 out of 4 rated this helpful - Rate this topic

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 ()
public Stream GetRequestStream ()
public override function GetRequestStream () : Stream
Not applicable.

Return Value

A Stream to use to write request data.
Exception typeCondition

ProtocolViolationException

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.

InvalidOperationException

The GetRequestStream method is called more than once.

-or-

TransferEncoding is set to a value and SendChunked is false.

NotSupportedException

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.

WebException

Abort was previously called.

-or-

The time-out period for the request expired.

-or-

An error occurred while processing the request.

ObjectDisposedException

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.

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.

You must set the value of the ContentLength property 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.

NoteNote:

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.

NoteNote:

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

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 = "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.set_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();
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();

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.