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 프록시를 사용하도록 설정되어 있고 DownloadFile, ListDirectory 또는 ListDirectoryDetails 이외의 FTP 명령을 사용하려고 한 경우

FTP 서버에 연결할 수 없는 경우

Method 속성이 UploadFile 또는 AppendFile로 설정되지 않은 경우

예제

다음 코드 예제에서는 요청의 데이터 스트림에 파일을 복사 하 고 데이터를 업로드 하 고 파일에 추가 하는 서버에 요청을 보내는 방법을 보여 줍니다.

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 하기 전에 요청 속성을 설정합니다. 스트림에 데이터를 쓴 후 요청을 보내기 전에 스트림을 닫아야 합니다.

속성을 또는 AppendFileMethodUploadFile 설정하지 않은 경우 스트림을 가져올 수 없습니다.

GetRequestStream 스트림을 기다리는 동안 블록을 선택합니다. 이를 방지하려면 대신 메서드를 BeginGetRequestStream 호출합니다 GetRequestStream.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

호출자 참고

이 메서드는 네트워크 트래픽을 생성합니다.

적용 대상

추가 정보