FtpWebRequest.EndGetRequestStream(IAsyncResult) Metoda

Definice

Ukončí čekající asynchronní operaci spuštěnou pomocí BeginGetRequestStream(AsyncCallback, Object).

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

Parametry

asyncResult
IAsyncResult

Objekt IAsyncResult vrácený při spuštění operace.

Návraty

Zapisovatelná Stream instance přidružená k této instanci.

Výjimky

asyncResult je null.

asyncResult nebyl získán voláním BeginGetRequestStream(AsyncCallback, Object)metody .

Tato metoda již byla volána pro operaci identifikovanou pomocí asyncResult.

Příklady

Následující příklad kódu ukazuje ukončení asynchronní operace pro získání streamu požadavku. Tento příklad kódu je součástí většího příkladu FtpWebRequest pro přehled třídy.

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

Poznámky

Pokud se operace nedokončila, EndGetRequestStream metoda ji blokuje, dokud se operace nedokončila. Pokud chcete zjistit, jestli se operace dokončila, zkontrolujte vlastnost před voláním IsCompletedEndGetRequestStream.

Kromě výjimek uvedených v části "Výjimky" EndGetRequestStream znovu vyvolá výjimky, které byly vyvolány při otevírání datového proudu pro zápis.

Poznámka

Tento člen poskytuje trasovací informace, když je ve vaší aplikaci povoleno trasování sítě. Další informace naleznete v tématu Trasování sítě v rozhraní .NET Framework.

Platí pro

Viz také