FtpWebRequest.EndGetRequestStream(IAsyncResult) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
结束由 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
。
未能通过调用 BeginGetRequestStream(AsyncCallback, Object) 获取 asyncResult
。
已为 asyncResult
标识的操作调用了此方法。
示例
下面的代码示例演示如何结束异步操作以获取请求的流。 此代码示例是为类概述提供的更大示例的 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;
}
}
注解
如果操作尚未完成,则 方法将 EndGetRequestStream 阻止,直到操作完成。 若要确定操作是否已完成,请在 IsCompleted 调用 EndGetRequestStream之前检查 属性。
除了“异常” EndGetRequestStream 中记录的异常之外,还会重新引发在打开流进行写入时引发的异常。
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪。