This documentation is archived and is not being maintained.

FtpWebRequest.BeginGetRequestStream Method

Begins asynchronously opening a request's content stream for writing.

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

<HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading := True)> _
Public Overrides Function BeginGetRequestStream ( _
	callback As AsyncCallback, _
	state As Object _
) As IAsyncResult


Type: System.AsyncCallback
An AsyncCallback delegate that references the method to invoke when the operation is complete.
Type: System.Object
A user-defined object that contains information about the operation. This object is passed to the callback delegate when the operation completes.

Return Value

Type: System.IAsyncResult
An IAsyncResult instance that indicates the status of the operation.


A previous call to this method or GetRequestStream has not yet completed.


A connection to the FTP server could not be established.


The Method property is not set to UploadFile.

You must complete the asynchronous operation by calling the EndGetRequestStream method. Typically, EndGetRequestStream is called by the method referenced by callback. To determine the state of the operation, check the properties in the IAsyncResult object returned by this method.

This method does not block while waiting for the stream. To block, call GetRequestStream in place of this method.

For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.


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


The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Notes to Callers

This method generates network traffic.

The following code example demonstrates beginning an asynchronous operation to get a request's stream. This code example is part of a larger example provided for the FtpWebRequest class overview.

' Command line arguments are two strings:
' 1. The url that is the name of the file being uploaded to the server.
' 2. The name of the file on the local machine.
Public Shared Sub Main(ByVal args() As String)
    ' Create a Uri instance with the specified URI string.
    ' If the URI is not correctly formed, the Uri constructor
    ' will throw an exception.
    Dim waitObject As ManualResetEvent

    Dim target As New Uri(args(0))
    Dim fileName As String = args(1)
    Dim state As New FtpState()
    Dim request As FtpWebRequest = CType(WebRequest.Create(target), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.UploadFile

    ' This example uses anonymous logon.
    ' The request is anonymous by default; the credential does not have to be specified. 
    ' The example specifies the credential only to
    ' control how actions are logged on the server.

    request.Credentials = New NetworkCredential("anonymous", "")

    ' Store the request in the object that we pass into the
    ' asynchronous operations.
    state.Request = request
    state.FileName = fileName

    ' Get the event to wait on.
    waitObject = state.OperationComplete

    ' Asynchronously get the stream for the file contents.
    request.BeginGetRequestStream(New AsyncCallback(AddressOf EndGetStreamCallback), state)

    ' Block the current thread until all operations are complete.

    ' The operations either completed or threw an exception.
    If state.OperationException IsNot Nothing Then
        Throw state.OperationException
        Console.WriteLine("The operation completed - {0}", state.StatusDescription)
    End If
End Sub

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.