Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

WebRequest.GetRequestStream Method

When overridden in a descendant class, returns a Stream for writing data to the Internet resource.

[Visual Basic]
Public Overridable Function GetRequestStream() As Stream
[C#]
public virtual Stream GetRequestStream();
[C++]
public: virtual Stream* GetRequestStream();
[JScript]
public function GetRequestStream() : Stream;

Return Value

A Stream for writing data to the Internet resource.

Exceptions

Exception Type Condition
NotSupportedException Any attempt is made to access the method, when the method is not overridden in a descendant class.

Remarks

The GetRequestStream method initiates a request to send data to the Internet resource and returns a Stream instance for sending data to the Internet resource.

The GetRequestStream method provides synchronous access to the Stream. For asynchronous access, use the BeginGetRequestStream and EndGetRequestStream methods.

Note   The WebRequest class is an abstract (MustInherit in Visual Basic) class. The actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

Example

[Visual Basic, C#, C++] The following example uses the GetRequestStream method to obtain a stream and then writes data that stream.

[Visual Basic] 


' Set the 'ContentType' property of the WebRequest.
myWebRequest.ContentType = "application/x-www-form-urlencoded"

' Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength = byteArray.Length
Dim newStream As Stream = myWebRequest.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)

' Close the Stream object.
newStream.Close()

' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()


[C#] 

            // Set the 'ContentType' property of the WebRequest.
            myWebRequest.ContentType="application/x-www-form-urlencoded";

            // Set the 'ContentLength' property of the WebRequest.
            myWebRequest.ContentLength=byteArray.Length;
            Stream newStream=myWebRequest.GetRequestStream();
            newStream.Write(byteArray,0,byteArray.Length);

            // Close the Stream object.
            newStream.Close();

            // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
            WebResponse myWebResponse=myWebRequest.GetResponse();
            

[C++] 
// Set the 'ContentType' property of the WebRequest.
myWebRequest->ContentType=S"application/x-www-form-urlencoded";

// Set the 'ContentLength' property of the WebRequest.
myWebRequest->ContentLength=byteArray->Length;
Stream* newStream=myWebRequest->GetRequestStream();
newStream->Write(byteArray, 0, byteArray->Length);

// Close the Stream Object*.
newStream->Close();

// Assign the response Object* of 'WebRequest' to a 'WebResponse' variable.
WebResponse* myWebResponse=myWebRequest->GetResponse();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

WebRequest Class | WebRequest Members | System.Net Namespace | Using Streams on the Network | BeginGetRequestStream

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.