Export (0) Print
Expand All

FtpWebRequest::EndGetRequestStream Method

Ends a pending asynchronous operation started with BeginGetRequestStream.

Namespace:  System.Net
Assembly:  System (in System.dll)

public:
virtual Stream^ EndGetRequestStream(
	IAsyncResult^ asyncResult
) override

Parameters

asyncResult
Type: System::IAsyncResult

The IAsyncResult object that was returned when the operation started.

Return Value

Type: System.IO::Stream
A writable Stream instance associated with this instance.

ExceptionCondition
ArgumentNullException

asyncResult is nullptr.

ArgumentException

asyncResult was not obtained by calling BeginGetRequestStream.

InvalidOperationException

This method was already called for the operation identified by asyncResult.

If the operation has not completed, the EndGetRequestStream method blocks until the operation completes. To determine whether the operation has completed, check the IsCompleted property before calling EndGetRequestStream.

In addition to the exceptions noted in "Exceptions," EndGetRequestStream rethrows exceptions that were thrown while opening the stream for writing.

NoteNote

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

The following code example demonstrates ending an asynchronous operation to get a request's stream. This code example is part of a larger example provided for the FtpWebRequest class overview.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft