FtpWebRequest.BeginGetResponse(AsyncCallback, Object) 方法

定义

开始以异步方式向 FTP 服务器发送请求并从 FTP 服务器接收响应。

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

参数

callback
AsyncCallback

一个 AsyncCallback 委托,它引用操作完成时要调用的方法。

state
Object

一个用户定义对象,其中包含操作的相关信息。 操作完成时,此对象传递给 callback 委托。

返回

IAsyncResult 实例,指示操作的状态。

例外

示例

下面的代码示例演示如何结束异步操作以获取请求的流,然后启动请求以获取响应。 此代码示例是为类概述提供的更大示例的 FtpWebRequest 一部分。

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

注解

必须通过调用 EndGetResponse 方法完成异步操作。 通常, EndGetResponse 由 引用 callback的方法调用。 若要确定操作的状态,检查 方法返回BeginGetResponseIAsyncResult 对象中的属性。

Proxy如果直接或在配置文件中设置了 属性,则通过指定的代理与 FTP 服务器的通信。

BeginGetResponse 在等待服务器响应时不会阻止。 若要阻止,请 GetResponse 调用 方法代替 BeginGetResponse

有关使用异步编程模型的详细信息,请参阅 异步调用同步方法

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪

注意

WebException如果引发 ,请使用 Response 异常的 和 Status 属性来确定来自服务器的响应。

调用方说明

此方法生成网络流量。

适用于

另请参阅