FtpWebRequest.BeginGetRequestStream(AsyncCallback, Object) Metoda

Definicja

Rozpoczyna asynchroniczne otwieranie strumienia zawartości żądania do zapisu.

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

Parametry

callback
AsyncCallback

Delegat AsyncCallback , który odwołuje się do metody wywoływania po zakończeniu operacji.

state
Object

Obiekt zdefiniowany przez użytkownika, który zawiera informacje o operacji. Ten obiekt jest przekazywany do delegata po zakończeniu callback operacji.

Zwraca

Wystąpienie IAsyncResult wskazujące stan operacji.

Wyjątki

Poprzednie wywołanie tej metody lub GetRequestStream() nie zostało jeszcze ukończone.

Nie można nawiązać połączenia z serwerem FTP.

Właściwość nie jest ustawiona Method na UploadFilewartość .

Przykłady

Poniższy przykład kodu przedstawia rozpoczęcie operacji asynchronicznej w celu pobrania strumienia żądania. Ten przykład kodu jest częścią większego przykładu udostępnionego FtpWebRequest do omówienia klasy.

// Command line arguments are two strings:
// 1. The url that is the name of the file being uploaded to the server.
// 2. The name of the file on the local machine.
//
static void Main()
{
   array<String^>^args = Environment::GetCommandLineArgs();

   // Create a Uri instance with the specified URI string.
   // If the URI is not correctly formed, the Uri constructor
   // will throw an exception.
   ManualResetEvent^ waitObject;
   Uri^ target = gcnew Uri( args[ 1 ] );
   String^ fileName = args[ 2 ];
   FtpState^ state = gcnew FtpState;
   FtpWebRequest ^ request = dynamic_cast<FtpWebRequest^>(WebRequest::Create( target ));
   request->Method = WebRequestMethods::Ftp::UploadFile;

   // This example uses anonymous logon.
   // The request is anonymous by default; the credential does not have to be specified. 
   // The example specifies the credential only to
   // control how actions are logged on the server.
   request->Credentials = gcnew NetworkCredential( "anonymous","janeDoe@contoso.com" );

   // Store the request in the object that we pass into the
   // asynchronous operations.
   state->Request = request;
   state->FileName = fileName;

   // Get the event to wait on.
   waitObject = state->OperationComplete;

   // Asynchronously get the stream for the file contents.
   request->BeginGetRequestStream( gcnew AsyncCallback( EndGetStreamCallback ), state );

   // Block the current thread until all operations are complete.
   waitObject->WaitOne();

   // The operations either completed or threw an exception.
   if ( state->OperationException != nullptr )
   {
      throw state->OperationException;
   }
   else
   {
      Console::WriteLine( "The operation completed - {0}", state->StatusDescription );
   }
}
// Command line arguments are two strings:
// 1. The url that is the name of the file being uploaded to the server.
// 2. The name of the file on the local machine.
//
public static void Main(string[] args)
{
    // Create a Uri instance with the specified URI string.
    // If the URI is not correctly formed, the Uri constructor
    // will throw an exception.
    ManualResetEvent waitObject;

    Uri target = new Uri (args[0]);
    string fileName = args[1];
    FtpState state = new FtpState();
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
    request.Method = WebRequestMethods.Ftp.UploadFile;

    // This example uses anonymous logon.
    // The request is anonymous by default; the credential does not have to be specified.
    // The example specifies the credential only to
    // control how actions are logged on the server.

    request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

    // Store the request in the object that we pass into the
    // asynchronous operations.
    state.Request = request;
    state.FileName = fileName;

    // Get the event to wait on.
    waitObject = state.OperationComplete;

    // Asynchronously get the stream for the file contents.
    request.BeginGetRequestStream(
        new AsyncCallback (EndGetStreamCallback),
        state
    );

    // Block the current thread until all operations are complete.
    waitObject.WaitOne();

    // The operations either completed or threw an exception.
    if (state.OperationException != null)
    {
        throw state.OperationException;
    }
    else
    {
        Console.WriteLine("The operation completed - {0}", state.StatusDescription);
    }
}

Uwagi

Należy wykonać operację asynchroniczną, wywołując metodę EndGetRequestStream . EndGetRequestStream Zazwyczaj jest wywoływana przez metodę przywołyną przez callbackmetodę . Aby określić stan operacji, sprawdź właściwości obiektu IAsyncResult zwróconego przez tę metodę.

Ta metoda nie blokuje podczas oczekiwania na strumień. Aby zablokować, wywołaj GetRequestStream metodę zamiast tej metody.

Aby uzyskać szczegółowe informacje na temat korzystania z modelu programowania asynchronicznego, zobacz Asynchroniczne wywoływanie metod synchronicznych.

Uwaga

Ten element członkowski generuje informacje ze śledzenia pod warunkiem włączenia funkcji śledzenia sieci w aplikacji. Aby uzyskać więcej informacji, zobacz Śledzenie sieci w .NET Framework.

Uwagi dotyczące wywoływania

Ta metoda generuje ruch sieciowy.

Dotyczy

Zobacz też