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

傳回

WebResponse 參考,包含 FtpWebResponse 執行個體。 這個物件包含 FTP 伺服器對要求的回應。

例外狀況

asyncResultnull

已針對 asyncResult 所識別的作業,呼叫這個方法。

使用 HTTP Proxy 時發生錯誤。

範例

下列程式碼範例示範結束非同步作業以取得回應。 此程式碼範例是類別概觀所提供較大範例的 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中的網路追蹤

給呼叫者的注意事項

此方法會產生網路流量。

適用於

另請參閱