次の方法で共有


FtpWebRequest.EndGetRequestStream(IAsyncResult) メソッド

定義

BeginGetRequestStream(AsyncCallback, Object)で開始された保留中の非同期操作を終了します。

public:
 override System::IO::Stream ^ EndGetRequestStream(IAsyncResult ^ asyncResult);
public override System.IO.Stream EndGetRequestStream(IAsyncResult asyncResult);
override this.EndGetRequestStream : IAsyncResult -> System.IO.Stream
Public Overrides Function EndGetRequestStream (asyncResult As IAsyncResult) As Stream

パラメーター

asyncResult
IAsyncResult

操作の開始時に返された IAsyncResult オブジェクト。

戻り値

このインスタンスに関連付けられている書き込み可能な Stream インスタンス。

例外

asyncResultnullです。

asyncResult は、 BeginGetRequestStream(AsyncCallback, Object)を呼び出すことによって取得されませんでした。

このメソッドは、 asyncResultによって識別される操作に対して既に呼び出されています。

次のコード例は、要求のストリームを取得する非同期操作を終了する方法を示しています。 このコード例は、 FtpWebRequest クラスの概要に関するより大きな例の一部です。

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

注釈

操作が完了していない場合、 EndGetRequestStream メソッドは操作が完了するまでブロックします。 操作が完了したかどうかを確認するには、EndGetRequestStreamを呼び出す前に、IsCompleted プロパティを確認します。

「例外」に示されている例外に加えて、 EndGetRequestStream 書き込みのためにストリームを開くときにスローされた例外を再スローします。

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

適用対象

こちらもご覧ください