FtpWebRequest.GetRequestStream Metoda

Definicja

Pobiera strumień używany do przekazywania danych do serwera 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

Zwraca

Wystąpienie z możliwością Stream zapisu używane do przechowywania danych do wysłania na serwer przez bieżące żądanie.

Wyjątki

BeginGetRequestStream(AsyncCallback, Object) został wywołany i nie został ukończony.

-lub-

Serwer proxy HTTP jest włączony i podjęto próbę użycia polecenia FTP innego niż DownloadFile, ListDirectorylub ListDirectoryDetails.

Nie można nawiązać połączenia z serwerem FTP.

Właściwość nie jest ustawiona Method na UploadFile lub AppendFile.

Przykłady

Poniższy przykład kodu przedstawia kopiowanie pliku do strumienia danych żądania i wysyłanie żądania do serwera w celu przekazania danych i dołączenia go do pliku.

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

Uwagi

Ustaw właściwości żądania przed wywołaniem GetRequestStream metody . Po zapisaniu danych do strumienia należy zamknąć strumień przed wysłaniem żądania.

Jeśli właściwość nie jest ustawiona Method na UploadFile lub AppendFile, nie możesz pobrać strumienia.

GetRequestStream bloki podczas oczekiwania na strumień. Aby temu zapobiec, wywołaj metodę BeginGetRequestStream zamiast GetRequestStreammetody .

Uwaga

Ten element członkowski generuje informacje ze śledzenia pod warunkiem włączenia funkcji śledzenia sieci w aplikacji. Aby uzyskać więcej informacji, zobacz Śledzenie sieci w .NET Framework.

Uwagi dotyczące wywoływania

Ta metoda generuje ruch sieciowy.

Dotyczy

Zobacz też