语言

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 写实例。

例外

asyncResult 是 null。

已为标识 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 将阻止操作完成。 若要确定操作是否已完成,请在调用IsCompleted之前检查EndGetRequestStream属性。

除了“异常” EndGetRequestStream 中所述的异常外,还重新引发在打开流进行写入时引发的异常。

注释

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

适用于

另请参阅