共用方式為


FtpWebRequest.BeginGetResponse(AsyncCallback, Object) 方法

定義

開始以非同步的方式傳送要求,並且接收來自 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參考的方法呼叫。 若要判斷作業的狀態,請檢查 方法所BeginGetResponse傳回之 物件中的IAsyncResult屬性。

Proxy如果已設定 屬性,則直接或在組態檔中,會透過指定的 Proxy 與 FTP 伺服器進行通訊。

BeginGetResponse 等候來自伺服器的回應時不會封鎖。 若要封鎖,請呼叫 GetResponse 方法來取代 BeginGetResponse

如需使用異步程序設計模型的詳細資訊,請參閱 以異步方式呼叫同步方法

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

注意

WebException如果擲回 ,請使用Response例外狀況的 和 Status 屬性來判斷伺服器的回應。

給呼叫者的注意事項

此方法會產生網路流量。

適用於

另請參閱