当前位置: 首页>>代码示例>>C#>>正文


C# FtpWebRequest.Abort方法代码示例

本文整理汇总了C#中System.Net.FtpWebRequest.Abort方法的典型用法代码示例。如果您正苦于以下问题:C# FtpWebRequest.Abort方法的具体用法?C# FtpWebRequest.Abort怎么用?C# FtpWebRequest.Abort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.FtpWebRequest的用法示例。


在下文中一共展示了FtpWebRequest.Abort方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

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;
                }
            }
        }
开发者ID:.NET开发者,项目名称:System.Net,代码行数:119,代码来源:FtpWebRequest.Abort


注:本文中的System.Net.FtpWebRequest.Abort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。