HttpWebRequest.EndGetRequestStream Method (IAsyncResult)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Ends an asynchronous request for a Stream object to use to write data.

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

Public Overrides Function EndGetRequestStream (
	asyncResult As IAsyncResult
) As Stream


Type: System.IAsyncResult

The pending request for a stream.

Return Value

Type: System.IO.Stream

A Stream to use to write request data.

Exception Condition

asyncResult is null.


The request did not complete, and no stream is available.


asyncResult was not returned by the current instance from a call to BeginGetRequestStream.


This method was called previously using asyncResult.


Abort was previously called.


An error occurred while processing the request.

The EndGetRequestStream method completes an asynchronous request for a stream that was started by the BeginGetRequestStream method. 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 writing data to 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.


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 EndGetRequestStream method to end an asynchronous request for a stream instance.

Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Threading
Imports Microsoft.VisualBasic

Class HttpWebRequestBeginGetRequest
    Public Shared allDone As New ManualResetEvent(False)

    Shared Sub Main()

        ' Create a new HttpWebRequest object.
        Dim request As HttpWebRequest = CType(WebRequest.Create(""), _

        ' Set the ContentType property.
        request.ContentType = "application/x-www-form-urlencoded"

        '  Set the Method property to 'POST' to post data to the URI.
        request.Method = "POST"

        ' Start the asynchronous operation.		
        Dim result As IAsyncResult = _
            CType(request.BeginGetRequestStream(AddressOf GetRequestStreamCallback, request), _

        ' Keep the main thread from continuing while the asynchronous
        ' operation completes. A real world application
        ' could do something useful such as updating its user interface. 
    End Sub ' Main

    Private Shared Sub GetRequestStreamCallback(ByVal asynchronousResult As IAsyncResult)
        Dim request As HttpWebRequest = CType(asynchronousResult.AsyncState, HttpWebRequest)

        ' End the operation
        Dim postStream As Stream = request.EndGetRequestStream(asynchronousResult)
        Console.WriteLine("Please enter the input data to be posted:")
        Dim postData As [String] = Console.ReadLine()

        '  Convert the string into byte array.
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

        ' Write to the stream.
        postStream.Write(byteArray, 0, postData.Length)

        ' Start the asynchronous operation to get the response
        Dim result As IAsyncResult = _
            CType(request.BeginGetResponse(AddressOf GetResponseCallback, request), _
    End Sub ' ReadRequestStreamCallback

    Private Shared Sub GetResponseCallback(ByVal asynchronousResult As IAsyncResult)
        Dim request As HttpWebRequest = CType(asynchronousResult.AsyncState, HttpWebRequest)

        '  Get the response.
        Dim response As HttpWebResponse = CType(request.EndGetResponse(asynchronousResult), _

        Dim streamResponse As Stream = response.GetResponseStream()
        Dim streamRead As New StreamReader(streamResponse)
        Dim responseString As String = streamRead.ReadToEnd()


        ' Close Stream object.

        ' Release the HttpWebResponse.
    End Sub ' ReadResponseCallback

End Class ' HttpWebRequest_BeginGetRequest

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top