共用方式為


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 Proxy 已啟用,但您嘗試使用 DownloadFileListDirectoryListDirectoryDetails 以外的 FTP 命令。

無法建立與 FTP 伺服器的連接。

範例

下列程式代碼範例示範如何將檔案複製到要求的數據流,並將要求傳送至伺服器以上傳數據並將它附加至檔案。

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 方法之前設定要求屬性。 將數據寫入數據流之後,您必須先關閉數據流,再傳送要求。

如果您尚未將 Method 屬性設定為 UploadFileAppendFile,則無法取得數據流。

GetRequestStream 等候數據流時封鎖。 若要避免這種情況,請呼叫 BeginGetRequestStream 方法來取代 GetRequestStream

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

給呼叫者的注意事項

此方法會產生網路流量。

適用於

另請參閱