FtpWebRequest.BeginGetRequestStream(AsyncCallback, Object) メソッド

定義

書き込みのために要求のコンテンツ ストリームを非同期的に開きます。

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

パラメーター

callback
AsyncCallback

操作が完了したときに呼び出すメソッドを参照する AsyncCallback デリゲート。

state
Object

操作に関する情報を含むユーザー定義オブジェクト。 このオブジェクトは、操作の完了時に callback デリゲートに渡されます。

返品

操作の状態を示す IAsyncResult インスタンス。

例外

このメソッドまたは GetRequestStream() の以前の呼び出しがまだ完了していません。

FTP サーバーへの接続を確立できませんでした。

Method プロパティは UploadFile に設定されていません。

次のコード例は、要求のストリームを取得する非同期操作を開始する方法を示しています。 このコード例は、 FtpWebRequest クラスの概要に関するより大きな例の一部です。

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

注釈

EndGetRequestStream メソッドを呼び出して、非同期操作を完了する必要があります。 通常、 EndGetRequestStream は、 callbackによって参照されるメソッドによって呼び出されます。 操作の状態を確認するには、このメソッドによって返される IAsyncResult オブジェクトのプロパティを確認します。

このメソッドは、ストリームを待機している間はブロックしません。 ブロックするには、このメソッドの代わりに GetRequestStream を呼び出します。

非同期プログラミング モデルの使用の詳細については、「非同期メソッドの 呼び出し」を参照してください。

Note

このメンバーは、アプリケーションでネットワーク トレースを有効にすると、トレース情報を出力します。 詳細については、「.NET Framework の Network Tracingを参照してください。

注意 (呼び出し元)

この方法では、ネットワーク トラフィックが生成されます。

適用対象

こちらもご覧ください