FtpWebRequest.BeginGetResponse(AsyncCallback, Object) Metodo

Definizione

Inizia l'invio di una richiesta e la ricezione di una risposta da un server FTP in modo asincrono.

public:
 override IAsyncResult ^ BeginGetResponse(AsyncCallback ^ callback, System::Object ^ state);
public override IAsyncResult BeginGetResponse (AsyncCallback? callback, object? state);
public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state);
override this.BeginGetResponse : AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginGetResponse (callback As AsyncCallback, state As Object) As IAsyncResult

Parametri

callback
AsyncCallback

Delegato AsyncCallback cui fa riferimento il metodo da richiamare al completamento dell'operazione.

state
Object

Oggetto definito dall'utente che contiene informazioni sull'operazione. Questo oggetto viene passato al delegato callback al completamento dell'operazione.

Restituisce

Istanza di IAsyncResult che indica lo stato dell'operazione.

Eccezioni

Il metodo GetResponse() oppure il metodo BeginGetResponse(AsyncCallback, Object) è gia stato chiamato per questa istanza.

Esempio

Nell'esempio di codice seguente viene illustrata la fine di un'operazione asincrona per ottenere il flusso di una richiesta e quindi l'avvio di una richiesta per ottenere la risposta. Questo esempio di codice fa parte di un esempio più ampio fornito per la panoramica della FtpWebRequest classe.

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

Commenti

È necessario completare l'operazione asincrona chiamando il EndGetResponse metodo . In genere, EndGetResponse viene chiamato dal metodo a cui fa callbackriferimento . Per determinare lo stato dell'operazione, controllare le proprietà nell'oggetto IAsyncResult restituito dal BeginGetResponse metodo .

Se la Proxy proprietà è impostata, direttamente o in un file di configurazione, le comunicazioni con il server FTP vengono effettuate tramite il proxy specificato.

BeginGetResponse non blocca durante l'attesa della risposta dal server. Per bloccare, chiamare il GetResponse metodo al posto di BeginGetResponse.

Per altre informazioni sull'uso del modello di programmazione asincrona, vedere Chiamata asincrona di metodi sincroni.

Questo membro genera informazioni di traccia quando viene abilitata la funzionalità di traccia di rete nell'applicazione in uso. Per altre informazioni, vedere Traccia di rete in .NET Framework.

Nota

Se viene generata un'eccezione WebException , utilizzare le Response proprietà e Status dell'eccezione per determinare la risposta dal server.

Note per i chiamanti

Questo metodo genera il traffico di rete.

Si applica a

Vedi anche