Condividi tramite


FtpWebRequest.GetRequestStream Metodo

Definizione

Recupera il flusso usato per caricare i dati su un 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

Restituisce

Istanza di Stream scrivibile usata per memorizzare dati da inviare al server mediante la richiesta corrente.

Eccezioni

Il metodo BeginGetRequestStream(AsyncCallback, Object) è stato chiamato e non è stato completato.

-oppure-

Un proxy HTTP è attivo e si è tentato di utilizzare un comando FTP diverso da DownloadFile, ListDirectory oppure ListDirectoryDetails.

Non è stato possibile stabilire una connessione al server FTP.

La proprietà Method non è impostata su UploadFile o su AppendFile.

Esempio

Nell'esempio di codice seguente viene illustrata la copia di un file nel flusso di dati di una richiesta e l'invio di una richiesta al server per caricare i dati e aggiungerli a un 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;
}

Commenti

Impostare le proprietà della richiesta prima di chiamare il GetRequestStream metodo. Dopo aver scritto i dati nel flusso, è necessario chiudere il flusso prima di inviare la richiesta.

Se non è stata impostata la Method proprietà su UploadFile o AppendFile, non è possibile ottenere il flusso.

GetRequestStream blocca durante l'attesa del flusso. Per evitare questo problema, chiamare il BeginGetRequestStream metodo al posto di GetRequestStream.

Nota

Questo membro genera informazioni di traccia quando viene abilitata la funzionalità di traccia di rete nell'applicazione in uso. Per altre informazioni, vedere Traccia di rete in .NET Framework.

Note per i chiamanti

Questo metodo genera il traffico di rete.

Si applica a

Vedi anche