FtpWebRequest.GetRequestStream Metoda

Definice

Načte datový proud použitý k nahrání dat na 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

Návraty

Zapisovatelná Stream instance používaná k ukládání dat, která mají být odeslána na server aktuálním požadavkem.

Výjimky

BeginGetRequestStream(AsyncCallback, Object) byla volána a nebyla dokončena.

-nebo-

Je povolený proxy server HTTP a pokusili jste se použít jiný příkaz FTP než DownloadFile, ListDirectorynebo ListDirectoryDetails.

Nelze navázat připojení k serveru FTP.

Vlastnost Method není nastavená na UploadFile hodnotu nebo AppendFile.

Příklady

Následující příklad kódu ukazuje zkopírování souboru do datového streamu požadavku a odeslání požadavku na server, aby se data nahrála a připojila k souboru.

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

Poznámky

Před voláním GetRequestStream metody nastavte vlastnosti požadavku. Po zápisu dat do datového proudu musíte datový proud před odesláním požadavku zavřít.

Pokud jste nenastavili Method vlastnost na UploadFile nebo AppendFile, nemůžete datový proud získat.

GetRequestStream bloky při čekání na datový proud. Chcete-li tomu zabránit, volejte BeginGetRequestStream místo metody metodu GetRequestStream.

Poznámka

Tento člen poskytuje trasovací informace, když je ve vaší aplikaci povoleno trasování sítě. Další informace naleznete v tématu Trasování sítě v rozhraní .NET Framework.

Poznámky pro volající

Tato metoda generuje síťový provoz.

Platí pro

Viz také