FtpWebRequest.BeginGetRequestStream(AsyncCallback, Object) Methode

Definition

Beginnt das asynchrone Öffnen des Inhaltsstreams einer Anforderung zum Schreiben.

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

Parameter

callback
AsyncCallback

Ein AsyncCallback-Delegat, der auf die Methode verweist, die bei Abschluss des Vorgangs aufgerufen werden soll.

state
Object

Ein benutzerdefiniertes Objekt, das Informationen über den Vorgang enthält. Dieses Objekt wird bei Abschluss des Vorgangs an den callback-Delegaten übergeben.

Gibt zurück

Eine IAsyncResult-Instanz, die den Status des Vorgangs angibt.

Ausnahmen

Ein vorheriger Aufruf dieser Methode oder GetRequestStream() wurde noch nicht abgeschlossen.

Es konnte keine Verbindung mit dem FTP-Server hergestellt werden.

Die Method-Eigenschaft ist nicht auf UploadFile festgelegt.

Beispiele

Das folgende Codebeispiel veranschaulicht das Starten eines asynchronen Vorgangs zum Abrufen des Datenstroms einer Anforderung. Dieses Codebeispiel ist Teil eines größeren Beispiels für die FtpWebRequest Klassenübersicht.

// 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);
    }
}

Hinweise

Sie müssen den asynchronen Vorgang abschließen, indem Sie die EndGetRequestStream -Methode aufrufen. In der Regel wird von der -Methode aufgerufen, EndGetRequestStream auf die verwiesen wird callback. Um den Zustand des Vorgangs zu bestimmen, überprüfen Sie die Eigenschaften in dem IAsyncResult von dieser Methode zurückgegebenen Objekt.

Diese Methode wird beim Warten auf den Stream nicht blockiert. Rufen Sie GetRequestStream zum Blockieren anstelle dieser Methode auf.

Ausführliche Informationen zur Verwendung des asynchronen Programmiermodells finden Sie unter Asynchrones Aufrufen synchroner Methoden.

Hinweis

Dieser Member gibt Ablaufverfolgungsinformationen aus, wenn Sie die Netzwerkablaufverfolgung in der Anwendung aktivieren. Weitere Informationen finden Sie unter Netzwerkablaufverfolgung im .NET Framework.

Hinweise für Aufrufer

Diese Methode generiert Netzwerkdatenverkehr.

Gilt für:

Weitere Informationen