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

操作の状態を示す IAsyncResult インスタンス。

例外

GetResponse() または BeginGetResponse(AsyncCallback, Object) は、このインスタンスに対して既に呼び出されています。

次のコード例では、要求のストリームを取得するための非同期操作を終了してから、応答を取得する要求を開始する方法を示します。 このコード例は、クラスの概要に関して提供されるより大きな例の 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呼び出されます。 操作の状態を確認するには、メソッドによって返されるオブジェクトの IAsyncResult プロパティを BeginGetResponse 確認します。

プロパティが Proxy 直接または構成ファイルで設定されている場合、FTP サーバーとの通信は、指定されたプロキシを介して行われます。

BeginGetResponse は、サーバーからの応答を待機している間はブロックしません。 ブロックするには、次のBeginGetResponse代わりにメソッドをGetResponse呼び出します。

非同期プログラミング モデルの使用の詳細については、「 同期メソッドの非同期呼び出し」を参照してください。

このメンバーは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。 詳細については、「.NET Frameworkのネットワーク トレース」を参照してください。

注意

a WebException がスローされた場合は、例外の Response プロパティと Status プロパティを使用して、サーバーからの応答を確認します。

注意 (呼び出し元)

この方法では、ネットワーク トラフィックが生成されます。

適用対象

こちらもご覧ください