FtpWebRequest.EndGetRequestStream(IAsyncResult) 方法

定义

结束由 BeginGetRequestStream(AsyncCallback, Object) 启动的挂起的异步操作。Ends a pending asynchronous operation started with 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

参数

asyncResult
IAsyncResult

在操作开始时返回的 IAsyncResult 对象。The IAsyncResult object that was returned when the operation started.

返回

Stream

与此实例关联的可写 Stream 实例。A writable Stream instance associated with this instance.

例外

asyncResultnullasyncResult is null.

未能通过调用 BeginGetRequestStream(AsyncCallback, Object) 获取 asyncResultasyncResult was not obtained by calling BeginGetRequestStream(AsyncCallback, Object).

已为 asyncResult 标识的操作调用了此方法。This method was already called for the operation identified by asyncResult.

示例

下面的代码示例演示如何结束异步操作以获取请求的流。The following code example demonstrates ending an asynchronous operation to get a request's stream. 此代码示例是为类概述提供的更大示例的一部分 FtpWebRequestThis code example is part of a larger example provided for the FtpWebRequest class overview.

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

注解

如果操作尚未完成,则方法将被 EndGetRequestStream 阻止,直到操作完成。If the operation has not completed, the EndGetRequestStream method blocks until the operation completes. 若要确定操作是否已完成,请 IsCompleted 在调用前检查 EndGetRequestStream 属性。To determine whether the operation has completed, check the IsCompleted property before calling EndGetRequestStream.

除了 "异常" 中所述的异常之外,还会重新 EndGetRequestStream 引发打开要写入的流时引发的异常。In addition to the exceptions noted in "Exceptions," EndGetRequestStream rethrows exceptions that were thrown while opening the stream for writing.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

适用于

另请参阅