FtpWebRequest.EndGetRequestStream(IAsyncResult) Methode

Definition

Beendet einen ausstehenden Vorgang, der mit BeginGetRequestStream(AsyncCallback, Object) gestartet wurde.

public:
 override System::IO::Stream ^ EndGetRequestStream(IAsyncResult ^ asyncResult);
public override System.IO.Stream EndGetRequestStream (IAsyncResult asyncResult);
override this.EndGetRequestStream : IAsyncResult -> System.IO.Stream
Public Overrides Function EndGetRequestStream (asyncResult As IAsyncResult) As Stream

Parameter

asyncResult
IAsyncResult

Das IAsyncResult-Objekt, das zu Beginn des Vorgangs zurückgegeben wurde.

Gibt zurück

Eine nicht schreibgeschützte Stream-Instanz, die dieser Instanz zugeordnet ist.

Ausnahmen

asyncResult ist null.

asyncResult wurde nicht durch den Aufruf von BeginGetRequestStream(AsyncCallback, Object) abgerufen.

Diese Methode wurde bereits für den durch asyncResult bezeichneten Vorgang aufgerufen.

Beispiele

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

private:
   static void EndGetStreamCallback( IAsyncResult^ ar )
   {
      FtpState^ state = dynamic_cast<FtpState^>(ar->AsyncState);
      Stream^ requestStream = nullptr;

      // End the asynchronous call to get the request stream.
      try
      {
         requestStream = state->Request->EndGetRequestStream( ar );

         // Copy the file contents to the request stream.
         const int bufferLength = 2048;
         array<Byte>^buffer = gcnew array<Byte>(bufferLength);
         int count = 0;
         int readBytes = 0;
         FileStream^ stream = File::OpenRead( state->FileName );
         do
         {
            readBytes = stream->Read( buffer, 0, bufferLength );
            requestStream->Write( buffer, 0, bufferLength );
            count += readBytes;
         }
         while ( readBytes != 0 );
         Console::WriteLine( "Writing {0} bytes to the stream.", count );

         // IMPORTANT: Close the request stream before sending the request.
         requestStream->Close();

         // Asynchronously get the response to the upload request.
         state->Request->BeginGetResponse( gcnew AsyncCallback( EndGetResponseCallback ), state );
      }
      // Return exceptions to the main application thread.
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Could not get the request stream." );
         state->OperationException = e;
         state->OperationComplete->Set();
         return;
      }
   }
private static void EndGetStreamCallback(IAsyncResult ar)
{
    FtpState state = (FtpState) ar.AsyncState;

    Stream requestStream = null;
    // End the asynchronous call to get the request stream.
    try
    {
        requestStream = state.Request.EndGetRequestStream(ar);
        // Copy the file contents to the request stream.
        const int bufferLength = 2048;
        byte[] buffer = new byte[bufferLength];
        int count = 0;
        int readBytes = 0;
        FileStream stream = File.OpenRead(state.FileName);
        do
        {
            readBytes = stream.Read(buffer, 0, bufferLength);
            requestStream.Write(buffer, 0, readBytes);
            count += readBytes;
        }
        while (readBytes != 0);
        Console.WriteLine ("Writing {0} bytes to the stream.", count);
        // IMPORTANT: Close the request stream before sending the request.
        requestStream.Close();
        // Asynchronously get the response to the upload request.
        state.Request.BeginGetResponse(
            new AsyncCallback (EndGetResponseCallback),
            state
        );
    }
    // Return exceptions to the main application thread.
    catch (Exception e)
    {
        Console.WriteLine("Could not get the request stream.");
        state.OperationException = e;
        state.OperationComplete.Set();
        return;
    }
}

Hinweise

Wenn der Vorgang nicht abgeschlossen wurde, blockiert die EndGetRequestStream -Methode, bis der Vorgang abgeschlossen ist. Um zu ermitteln, ob der Vorgang abgeschlossen wurde, überprüfen Sie die IsCompleted -Eigenschaft, bevor Sie aufrufen EndGetRequestStream.

Zusätzlich zu den in "Ausnahmen" EndGetRequestStream notierten Ausnahmen werden Ausnahmen erneut ausgeführt, die beim Öffnen des Datenstroms zum Schreiben ausgelöst wurden.

Hinweis

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

Gilt für:

Weitere Informationen