FtpWebRequest.EndGetResponse(IAsyncResult) 方法

定义

结束由 BeginGetResponse(AsyncCallback, Object) 启动的挂起的异步操作。

public:
 override System::Net::WebResponse ^ EndGetResponse(IAsyncResult ^ asyncResult);
public override System.Net.WebResponse EndGetResponse (IAsyncResult asyncResult);
override this.EndGetResponse : IAsyncResult -> System.Net.WebResponse
Public Overrides Function EndGetResponse (asyncResult As IAsyncResult) As WebResponse

参数

asyncResult
IAsyncResult

在操作开始时返回的 IAsyncResult

返回

包含一个 FtpWebResponse 实例的 WebResponse 引用。 此对象包含 FTP 服务器对请求的响应。

例外

asyncResultnull

未能通过调用 BeginGetResponse(AsyncCallback, Object) 获取 asyncResult

已为 asyncResult 标识的操作调用了此方法。

使用 HTTP 代理时出错。

示例

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

// The EndGetResponseCallback method  
// completes a call to BeginGetResponse.
static void EndGetResponseCallback( IAsyncResult^ ar )
{
   FtpState^ state = dynamic_cast<FtpState^>(ar->AsyncState);
   FtpWebResponse ^ response = nullptr;
   try
   {
      response = dynamic_cast<FtpWebResponse^>(state->Request->EndGetResponse( ar ));
      response->Close();
      state->StatusDescription = response->StatusDescription;

      // Signal the main application thread that 
      // the operation is complete.
      state->OperationComplete->Set();
   }
   // Return exceptions to the main application thread.
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Error getting response." );
      state->OperationException = e;
      state->OperationComplete->Set();
   }
}
// The EndGetResponseCallback method
// completes a call to BeginGetResponse.
private static void EndGetResponseCallback(IAsyncResult ar)
{
    FtpState state = (FtpState) ar.AsyncState;
    FtpWebResponse response = null;
    try
    {
        response = (FtpWebResponse) state.Request.EndGetResponse(ar);
        response.Close();
        state.StatusDescription = response.StatusDescription;
        // Signal the main application thread that
        // the operation is complete.
        state.OperationComplete.Set();
    }
    // Return exceptions to the main application thread.
    catch (Exception e)
    {
        Console.WriteLine ("Error getting response.");
        state.OperationException = e;
        state.OperationComplete.Set();
    }
}

注解

如果在调用方法时 EndGetResponse 操作尚未完成,将一直阻止, EndGetResponse 直到操作完成。 若要防止阻塞,请在 IsCompleted 调用 EndGetResponse之前检查 属性。

除了“异常”中记录的异常之外, EndGetResponse 还会重新引发与服务器通信时引发的异常。

备注

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

调用方说明

此方法生成网络流量。

适用于

另请参阅