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 封鎖直到作業完成為止。 若要判斷作業是否已完成,請在呼叫 EndGetRequestStream之前檢查 IsCompleted 屬性。
除了「例外狀況」中所指出的例外狀況之外, EndGetRequestStream 還會重新擲回開啟數據流以進行寫入時擲回的例外狀況。
注意
在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤。