Export (0) Print
Expand All

FtpWebRequest.BeginGetRequestStream Method

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

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

public override IAsyncResult BeginGetRequestStream (
	AsyncCallback callback,
	Object state
)
public IAsyncResult BeginGetRequestStream (
	AsyncCallback callback, 
	Object state
)
public override function BeginGetRequestStream (
	callback : AsyncCallback, 
	state : Object
) : IAsyncResult
Not applicable.

Parameters

callback

An AsyncCallback delegate that references the method to invoke when the operation is complete.

state

A user-defined object that contains information about the operation. This object is passed to the callback delegate when the operation completes.

Return Value

An IAsyncResult instance that indicates the status of the operation.

Exception typeCondition

InvalidOperationException

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

WebException

A connection to the FTP server could not be established.

ProtocolViolationException

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.

NoteNote:

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

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 static void Main(string[] args)
{
    // Create a Uri instance with the specified URI string.
    // If the URI is not correctly formed, the Uri constructor
    // will throw an exception.
    ManualResetEvent waitObject;
    
    Uri target = new Uri (args[0]);
    string fileName = args[1];
    FtpState state = new FtpState();
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
    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","janeDoe@contoso.com");
    
    // 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 (EndGetStreamCallback), 
        state
    );
    
    // Block the current thread until all operations are complete.
    waitObject.WaitOne();
    
    // The operations either completed or threw an exception.
    if (state.OperationException != null)
    {
        throw state.OperationException;
    }
    else
    {
        Console.WriteLine("The operation completed - {0}", state.StatusDescription);
    }
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, 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

Community Additions

ADD
Show:
© 2014 Microsoft