Bagikan melalui


FtpWebRequest.GetRequestStream Metode

Definisi

Mengambil aliran yang digunakan untuk mengunggah data ke server 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

Mengembalikan

Instans bisa-tulis Stream yang digunakan untuk menyimpan data yang akan dikirim ke server oleh permintaan saat ini.

Pengecualian

BeginGetRequestStream(AsyncCallback, Object) telah dipanggil dan belum selesai.

-atau-

Proksi HTTP diaktifkan, dan Anda mencoba menggunakan perintah FTP selain DownloadFile, , ListDirectoryatau ListDirectoryDetails.

Sambungan ke server FTP tidak dapat dibuat.

Properti Method tidak diatur ke UploadFile atau AppendFile.

Contoh

Contoh kode berikut menunjukkan penyalinan file ke aliran data permintaan dan mengirim permintaan ke server untuk mengunggah data dan menambahkannya ke file.

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

Keterangan

Atur properti permintaan sebelum memanggil GetRequestStream metode . Setelah menulis data ke aliran, Anda harus menutup aliran sebelum mengirim permintaan.

Jika Anda belum mengatur properti ke MethodUploadFile atau AppendFile, Anda tidak bisa mendapatkan streaming.

GetRequestStream memblokir sambil menunggu aliran. Untuk mencegah hal ini, panggil BeginGetRequestStream metode sebagai pengganti GetRequestStream.

Catatan

Anggota ini mengeluarkan informasi pelacakan saat Anda mengaktifkan pelacakan jaringan di aplikasi Anda. Untuk informasi selengkapnya, lihat Pelacakan Jaringan di .NET Framework.

Catatan Bagi Pemanggil

Metode ini menghasilkan lalu lintas jaringan.

Berlaku untuk

Lihat juga