FtpWebRequest.EndGetResponse(IAsyncResult) Method

Definition

Ends a pending asynchronous operation started with BeginGetResponse(AsyncCallback, Object).

public:
 override System::Net::WebResponse ^ EndGetResponse(IAsyncResult ^ asyncResult);
public override System.Net.WebResponse EndGetResponse (IAsyncResult asyncResult);
override this.EndGetResponse : IAsyncResult -> System.Net.WebResponse
Public Overrides Function EndGetResponse (asyncResult As IAsyncResult) As WebResponse

Parameters

asyncResult
IAsyncResult

The IAsyncResult that was returned when the operation started.

Returns

A WebResponse reference that contains an FtpWebResponse instance. This object contains the FTP server's response to the request.

Exceptions

asyncResult is null.

asyncResult was not obtained by calling BeginGetResponse(AsyncCallback, Object).

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

An error occurred using an HTTP proxy.

Examples

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

// The EndGetResponseCallback method  
// completes a call to BeginGetResponse.
static void EndGetResponseCallback( IAsyncResult^ ar )
{
   FtpState^ state = dynamic_cast<FtpState^>(ar->AsyncState);
   FtpWebResponse ^ response = nullptr;
   try
   {
      response = dynamic_cast<FtpWebResponse^>(state->Request->EndGetResponse( ar ));
      response->Close();
      state->StatusDescription = response->StatusDescription;

      // Signal the main application thread that 
      // the operation is complete.
      state->OperationComplete->Set();
   }
   // Return exceptions to the main application thread.
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Error getting response." );
      state->OperationException = e;
      state->OperationComplete->Set();
   }
}
// The EndGetResponseCallback method
// completes a call to BeginGetResponse.
private static void EndGetResponseCallback(IAsyncResult ar)
{
    FtpState state = (FtpState) ar.AsyncState;
    FtpWebResponse response = null;
    try
    {
        response = (FtpWebResponse) state.Request.EndGetResponse(ar);
        response.Close();
        state.StatusDescription = response.StatusDescription;
        // Signal the main application thread that
        // the operation is complete.
        state.OperationComplete.Set();
    }
    // Return exceptions to the main application thread.
    catch (Exception e)
    {
        Console.WriteLine ("Error getting response.");
        state.OperationException = e;
        state.OperationComplete.Set();
    }
}

Remarks

If the operation has not completed at the time the EndGetResponse method is called, EndGetResponse blocks until the operation completes. To prevent blocking, check the IsCompleted property before calling EndGetResponse.

In addition to the exceptions noted in "Exceptions," EndGetResponse rethrows exceptions that were thrown while communicating with the server.

Note

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

Notes to Callers

This method generates network traffic.

Applies to

See also