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

注釈

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

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

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

Note

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

注意 (呼び出し元)

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

適用対象

こちらもご覧ください