码迷,mamicode.com
首页 > Web开发 > 详细

DownLoadPageFromWeb

时间:2016-08-03 01:29:39      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

DownLoadPageFromWeb

class Program
    {
        /// <summary>
        /// 异步回调函数
        /// </summary>
        /// <param name="ar"></param>
        static void DownloadFinished(IAsyncResult ar)
        {
            try
            {
                DownLoadTask task = ar.AsyncState as DownLoadTask;
            WebResponse response = task.WebRequestObject.EndGetResponse(ar);
                string FileContent="";
                using(StreamReader reader=new StreamReader(response.GetResponseStream(),Encoding.GetEncoding("gb2312")))
                {
                    FileContent = reader.ReadToEnd();
                }

                using (StreamWriter writer = new StreamWriter(new FileStream(task.SaveToFileName, FileMode.Create), Encoding.GetEncoding("gb2312")))
                    {
                        writer.Write(FileContent);
                    }
               
                MessageBox.Show(string.Format("“{0}”下载完成!", task.SaveToFileName));
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }



        static void Main(string[] args)
        {
            string UserInputUrl = "";
            string FileName = "";
            Console.WriteLine("输入URL启动一个异步下载Web文件任务,输入quit退出。");
            do
            {

                Console.Write("\n输入Web文件的URL:");
                UserInputUrl = Console.ReadLine();
                if (string.IsNullOrEmpty(UserInputUrl))
                {
                    Console.WriteLine("不能输入一个空的URL字串");
                    continue;
                }
                if (UserInputUrl.ToLower() == "quit")
                    break;
                Console.Write("输入要保存的文件名:");
                FileName = Console.ReadLine();
                if (string.IsNullOrEmpty(FileName))
                {
                    Console.WriteLine("不能输入一个空的URL字串");
                    continue;
                }
                if (FileName.ToLower() == "quit")
                    break;

                if (UserInputUrl.ToLower() == "quit")
                    break;
                try
                {
                    Uri webFileUri = new Uri(UserInputUrl);
                    WebRequest webRequest = WebRequest.Create(webFileUri);
                    DownLoadTask task = new DownLoadTask { SaveToFileName = FileName, WebRequestObject = webRequest };
                    Console.WriteLine("已在后台启动下载{0}", FileName);
                    webRequest.BeginGetResponse(DownloadFinished, task);

                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message);
                }



            } while (true);

            Console.WriteLine("\n敲任意键结束本示例程序...");
         
            Console.ReadKey();
        }
    }

 

 

异步编程概述

https://msdn.microsoft.com/zh-cn/library/ms228963(v=vs.90).aspx

DownLoadPageFromWeb

标签:

原文地址:http://www.cnblogs.com/FLY_DREAM/p/5731279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!