Sdílet prostřednictvím


FtpWebRequest.BeginGetRequestStream(AsyncCallback, Object) Metoda

Definice

Začne asynchronně otevírat stream obsahu požadavku pro zápis.

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

Delegát AsyncCallback , který odkazuje na metodu vyvolání po dokončení operace.

state
Object

Uživatelem definovaný objekt, který obsahuje informace o operaci. Tento objekt se po dokončení operace předá callback delegátu.

Návraty

Instance IAsyncResult , která označuje stav operace.

Výjimky

Předchozí volání této metody nebo GetRequestStream() ještě nebyla dokončena.

Nelze navázat připojení k serveru FTP.

Vlastnost Method není nastavená na UploadFilehodnotu .

Příklady

Následující příklad kódu ukazuje zahájení asynchronní operace pro získání streamu požadavku. Tento příklad kódu je součástí většího příkladu FtpWebRequest pro přehled třídy.

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

Poznámky

Asynchronní operaci musíte dokončit voláním EndGetRequestStream metody . Obvykle je volána metodou, EndGetRequestStream na kterou odkazuje .callback Chcete-li zjistit stav operace, zkontrolujte vlastnosti objektu IAsyncResult vráceného touto metodou.

Tato metoda neblokuje při čekání na datový proud. Chcete-li blokovat, zavolejte GetRequestStream místo této metody.

Podrobné informace o použití asynchronního programovacího modelu najdete v tématu Asynchronní volání synchronních metod.

Poznámka

Tento člen poskytuje trasovací informace, když je ve vaší aplikaci povoleno trasování sítě. Další informace naleznete v tématu Trasování sítě v rozhraní .NET Framework.

Poznámky pro volající

Tato metoda generuje síťový provoz.

Platí pro

Viz také