FtpWebRequest.Abort 메서드

정의

비동기 FTP 작업을 종료합니다.

public:
 override void Abort();
public override void Abort ();
override this.Abort : unit -> unit
Public Overrides Sub Abort ()

예제

다음 코드 예제에서는 사용자가 로컬 디렉터리에서 서버로 파일의 비동기 업로드를 종료하는 방법을 보여 줍니다.

public:
   void AbortUpload( String^ fileName, String^ serverUri )
   {
      request = dynamic_cast<FtpWebRequest^>(WebRequest::Create( serverUri ));
      request->Method = WebRequestMethods::Ftp::UploadFile;

      // Get the file to be uploaded and convert it to bytes.
      StreamReader^ sourceStream = gcnew StreamReader( fileName );
      fileContents = Encoding::UTF8->GetBytes( sourceStream->ReadToEnd() );
      sourceStream->Close();

      // Set the content length to the number of bytes in the file.
      request->ContentLength = fileContents->Length;

      // Asynchronously get the stream for the file contents.
      IAsyncResult^ ar = request->BeginGetRequestStream( gcnew AsyncCallback( this, &AsynchronousFtpUpLoader::EndGetStreamCallback ), nullptr );
      Console::WriteLine( "Getting the request stream asynchronously..." );
      Console::WriteLine( "Press 'a' to abort the upload or any other key to continue" );
      String^ input = Console::ReadLine();
      if ( input->Equals( "a" ) )
      {
         request->Abort();
         Console::WriteLine( "Request aborted" );
         return;
      }
   }
public class ApplicationMain
{
    public static void Main()
    {
          ManualResetEvent wait = new ManualResetEvent(false);
          AsynchronousFtpUpLoader uploader = new AsynchronousFtpUpLoader(wait);
          uploader.AllowAbortUpload("out.txt", "ftp://sharriso1/ftptests.txt");
          wait.WaitOne();
          if (uploader.AsyncException != null)
          {
                Console.WriteLine(uploader.AsyncException.ToString());
          }
    }
}
    public class AsynchronousFtpUpLoader
    {
        ManualResetEvent wait;
        FtpWebRequest request;
        byte [] fileContents;
        Exception asyncException = null;

        public AsynchronousFtpUpLoader(ManualResetEvent wait)
        {
            this.wait = wait;
        }

        public Exception AsyncException
        {
            get { return asyncException;}
        }

        private void EndGetStreamCallback(IAsyncResult ar)
        {
            Stream requestStream = null;
            // End the asynchronous call to get the request stream.
            try
            {
                requestStream = request.EndGetRequestStream(ar);
            }
            // Return exceptions to the main application thread.
            catch (Exception e)
            {
                Console.WriteLine("Could not get the request stream.");
                asyncException = e;
                wait.Set();
                return;
            }
            // Write the file contents to the request stream.
            requestStream.Write(fileContents, 0, fileContents.Length);
            Console.WriteLine ("Writing {0} bytes to the stream.", fileContents.Length);
            // IMPORTANT: Close the request stream before sending the request.
            requestStream.Close();
        }

        // The EndGetResponseCallback method
        // completes a call to BeginGetResponse.
        private void EndGetResponseCallback(IAsyncResult ar)
        {
            FtpWebResponse response = null;
            try
            {
                response = (FtpWebResponse) request.EndGetResponse(ar);
            }
            // Return exceptions to the main application thread.
            catch (Exception e)
            {
                Console.WriteLine ("Error getting response.");
                asyncException = e;
                wait.Set();
            }
            Console.WriteLine("Upload status: {0}",response.StatusDescription);
            // Signal the application thread that this operation is complete.
            wait.Set();
        }
        internal void AbortRequest(FtpWebRequest request)
        {
            request.Abort();
            Console.WriteLine("Request aborted!");
            wait.Set();
        }

       public void AllowAbortUpload(string fileName, string serverUri)
       {
            request = (FtpWebRequest)WebRequest.Create(serverUri);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            // Get the file to be uploaded and convert it to bytes.
            StreamReader sourceStream = new StreamReader(fileName);
            fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            // Set the content length to the number of bytes in the file.
            request.ContentLength = fileContents.Length;
            // Asynchronously get the stream for the file contents.
            IAsyncResult ar = request.BeginGetRequestStream(
                new AsyncCallback (EndGetStreamCallback), null);
             while (!ar.IsCompleted)
            {
                Console.WriteLine("Press 'a' to abort writing to the request stream. Press any other key to continue...");
                string input = Console.ReadLine();
                if (input == "a")
                {
                    AbortRequest(request);
                    return;
                }
            }
            Console.WriteLine("Sending the request asynchronously...");
            IAsyncResult responseAR = request.BeginGetResponse(
                new AsyncCallback (EndGetResponseCallback), null);

            while (!responseAR.IsCompleted)
            {
                Console.WriteLine("Press 'a' to abort the upload. Press any other key to continue.");
                string input = Console.ReadLine();
                if (input == "a")
                {
                    AbortRequest(request);
                    return;
                }
            }
        }

설명

진행 중인 작업이 없으면 이 메서드는 아무 작업도 수행하지 않습니다. 파일 전송이 진행 중인 경우 이 메서드는 전송을 종료합니다.

참고

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

적용 대상

추가 정보