FtpWebRequest.BeginGetRequestStream(AsyncCallback, Object) Méthode

Définition

Commence l'ouverture asynchrone du flux de contenu d'une demande en vue de l'écriture.

public:
 override IAsyncResult ^ BeginGetRequestStream(AsyncCallback ^ callback, System::Object ^ state);
public override IAsyncResult BeginGetRequestStream (AsyncCallback? callback, object? state);
public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state);
override this.BeginGetRequestStream : AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginGetRequestStream (callback As AsyncCallback, state As Object) As IAsyncResult

Paramètres

callback
AsyncCallback

Délégué AsyncCallback qui fait référence à la méthode à appeler quand l'opération est terminée.

state
Object

Objet défini par l'utilisateur qui comporte des informations sur l'opération. Cet objet est passé au délégué callback quand l'opération se termine.

Retours

Instance de IAsyncResult indiquant l'état de l'opération.

Exceptions

Un appel précédent à cette méthode ou à GetRequestStream() n'est pas encore terminé.

Une connexion au serveur FTP n'a pas pu être établie.

La propriété Method n'a pas la valeur UploadFile.

Exemples

L’exemple de code suivant illustre le début d’une opération asynchrone pour obtenir le flux d’une requête. Cet exemple de code fait partie d’un exemple plus large fourni pour la vue d’ensemble de la FtpWebRequest classe.

// 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.
//
static void Main()
{
   array<String^>^args = Environment::GetCommandLineArgs();

   // 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 = gcnew Uri( args[ 1 ] );
   String^ fileName = args[ 2 ];
   FtpState^ state = gcnew FtpState;
   FtpWebRequest ^ request = dynamic_cast<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 = gcnew 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( gcnew 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 != nullptr )
   {
      throw state->OperationException;
   }
   else
   {
      Console::WriteLine( "The operation completed - {0}", state->StatusDescription );
   }
}
// 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);
    }
}

Remarques

Vous devez terminer l’opération asynchrone en appelant la EndGetRequestStream méthode . Généralement, EndGetRequestStream est appelé par la méthode référencée par callback. Pour déterminer l’état de l’opération, case activée les propriétés de l’objet IAsyncResult retourné par cette méthode.

Cette méthode ne bloque pas en attendant le flux. Pour bloquer, appelez GetRequestStream à la place de cette méthode.

Pour plus d’informations sur l’utilisation du modèle de programmation asynchrone, consultez Appel de méthodes synchrones de manière asynchrone.

Notes

Ce membre génère des informations de traçage lorsque vous activez le traçage réseau dans votre application. Pour plus d’informations, consultez Suivi réseau dans .NET Framework.

Notes pour les appelants

Cette méthode génère du trafic réseau.

S’applique à

Voir aussi