FtpWebRequest.GetRequestStream メソッド

定義

FTP サーバーにデータをアップロードするために使用するストリームを取得します。

public:
 override System::IO::Stream ^ GetRequestStream();
public override System.IO.Stream GetRequestStream ();
override this.GetRequestStream : unit -> System.IO.Stream
Public Overrides Function GetRequestStream () As Stream

戻り値

現在の要求でサーバーに送信されるデータを格納するために使用する、書き込み可能な Stream インスタンス。

例外

BeginGetRequestStream(AsyncCallback, Object) が呼び出されており、完了していません。

- または -

HTTP プロキシが有効で、DownloadFileListDirectory、または ListDirectoryDetails 以外の FTP コマンドを使用しようとしました。

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

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

次のコード例では、ファイルを要求のデータ ストリームにコピーし、サーバーに要求を送信してデータをアップロードし、それをファイルに追加する方法を示します。

static bool AppendFileOnServer( String^ fileName, Uri^ serverUri )
{
   // The URI described by serverUri should use the ftp:// scheme.
   // It contains the name of the file on the server.
   // Example: ftp://contoso.com/someFile.txt. 
   // The fileName parameter identifies the file containing 
   // the data to be appended to the file on the server.
   if ( serverUri->Scheme != Uri::UriSchemeFtp )
   {
      return false;
   }

   // Get the object used to communicate with the server.
   FtpWebRequest^ request = dynamic_cast<FtpWebRequest^>(WebRequest::Create( serverUri ));
   request->Method = WebRequestMethods::Ftp::AppendFile;
   StreamReader^ sourceStream = gcnew StreamReader( fileName );
   array<Byte>^fileContents = Encoding::UTF8->GetBytes( sourceStream->ReadToEnd() );
   sourceStream->Close();
   request->ContentLength = fileContents->Length;

   // This example assumes the FTP site uses anonymous logon.
   request->Credentials = gcnew NetworkCredential( "anonymous","janeDoe@contoso.com" );
   Stream^ requestStream = request->GetRequestStream();
   requestStream->Write( fileContents, 0, fileContents->Length );
   requestStream->Close();
   FtpWebResponse^ response = dynamic_cast<FtpWebResponse^>(request->GetResponse());
   Console::WriteLine( "Append status: {0}", response->StatusDescription );
   response->Close();
   return true;
}
public static bool AppendFileOnServer(string fileName, Uri serverUri)
{
    // The URI described by serverUri should use the ftp:// scheme.
    // It contains the name of the file on the server.
    // Example: ftp://contoso.com/someFile.txt.
    // The fileName parameter identifies the file containing
    // the data to be appended to the file on the server.

    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.Method = WebRequestMethods.Ftp.AppendFile;

    StreamReader sourceStream = new StreamReader(fileName);
    byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    request.ContentLength = fileContents.Length;

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);
    requestStream.Close();
    FtpWebResponse response = (FtpWebResponse) request.GetResponse();

    Console.WriteLine("Append status: {0}",response.StatusDescription);

    response.Close();
    return true;
}

注釈

メソッドを呼び出す前に、要求プロパティを設定します GetRequestStream 。 ストリームにデータを書き込んでから、要求を送信する前にストリームを閉じる必要があります。

プロパティを または AppendFileMethodUploadFile設定していない場合は、ストリームを取得できません。

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

Note

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

注意 (呼び出し元)

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

適用対象

こちらもご覧ください