/// <summary>
        /// 缺省窗口过程
        /// </summary>
        /// <param name="m">消息</param>
        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_RUN:
                    {
                        this.MessageText = "正在下载Web文件 " + this.Url;

                        if (string.IsNullOrEmpty(this.TempFielname) == true)
                            this.TempFielname = Path.GetTempFileName();

                        MyWebClient webClient = new MyWebClient();

                        if (string.IsNullOrEmpty(this.IfModifySince) == false)
                            webClient.IfModifiedSince = this.IfModifySince;

                        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
                        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
                        try
                        {


                            webClient.DownloadFileAsync(new Uri(this.Url, UriKind.Absolute),
                                this.TempFielname, null);
                            while (true)
                            {
                                Application.DoEvents();
                                if (eventComplete.WaitOne(100) == true)
                                    break;
                            }
                        }
                        catch (Exception ex)
                        {

                            // File.Delete(this.OutputFielname);

                            this.ErrorInfo = "下载 " + this.Url + " 时发生错误 :" + ex.Message;
                            this.Close();
                            return;
                        }
                        finally
                        {
                            if (webClient.ResponseHeaders != null)
                                this.LastModified = webClient.ResponseHeaders["Last-Modified"];
                            webClient.CancelAsync();
                            webClient.Dispose();
                        }

                        if (this.e == null
    || this.e.Cancelled == true)
                        {
                            try
                            {
                                File.Delete(this.TempFielname);
                            }
                            catch
                            {
                            }

                            this.ErrorInfo = "下载 " + this.Url + " 被取消";
                        }

                        if (this.e != null)
                        {
                            if (e.Error != null)
                            {
                                try
                                {
                                    File.Delete(this.TempFielname);
                                }
                                catch
                                {
                                }
                                // TODO: 准备处理304响应
                                var webException = this.e.Error as WebException;
                                if (null != webException)
                                {
                                    var httWebResponse = webException.Response as HttpWebResponse;
                                    if (null != httWebResponse)
                                    {
                                        if (HttpStatusCode.NotModified == httWebResponse.StatusCode)
                                        {
                                            this.NotModified = true;
                                        }
                                    }
                                }

                                this.ErrorInfo = "下载 " + this.Url + " 过程发生错误: " + ExceptionUtil.GetExceptionMessage(this.e.Error);
                            }
                            else
                            {
                                File.Copy(this.TempFielname, this.OutputFilename, true);
                                try
                                {
                                    File.Delete(this.TempFielname);
                                }
                                catch
                                {
                                }
                            }
                        }

                        this.Close();
                    }
                    return;
            }
            base.DefWndProc(ref m);
        }
Exemple #2
0
        /// <summary>
        /// 缺省窗口过程
        /// </summary>
        /// <param name="m">消息</param>
        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
            case WM_RUN:
            {
                this.MessageText = "正在下载Web文件 " + this.Url;

                if (string.IsNullOrEmpty(this.TempFielname) == true)
                {
                    this.TempFielname = Path.GetTempFileName();
                }

                MyWebClient webClient = new MyWebClient();

                if (string.IsNullOrEmpty(this.IfModifySince) == false)
                {
                    webClient.IfModifiedSince = this.IfModifySince;
                }

                webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
                try
                {
                    webClient.DownloadFileAsync(new Uri(this.Url, UriKind.Absolute),
                                                this.TempFielname, null);
                    while (true)
                    {
                        Application.DoEvents();
                        if (eventComplete.WaitOne(100) == true)
                        {
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    // File.Delete(this.OutputFielname);

                    this.ErrorInfo = "下载 " + this.Url + " 时发生错误 :" + ex.Message;
                    this.Close();
                    return;
                }
                finally
                {
                    if (webClient.ResponseHeaders != null)
                    {
                        this.LastModified = webClient.ResponseHeaders["Last-Modified"];
                    }
                    webClient.CancelAsync();
                    webClient.Dispose();
                }

                if (this.e == null ||
                    this.e.Cancelled == true)
                {
                    try
                    {
                        File.Delete(this.TempFielname);
                    }
                    catch
                    {
                    }

                    this.ErrorInfo = "下载 " + this.Url + " 被取消";
                }

                if (this.e != null)
                {
                    if (e.Error != null)
                    {
                        try
                        {
                            File.Delete(this.TempFielname);
                        }
                        catch
                        {
                        }
                        // TODO: 准备处理304响应
                        var webException = this.e.Error as WebException;
                        if (null != webException)
                        {
                            var httWebResponse = webException.Response as HttpWebResponse;
                            if (null != httWebResponse)
                            {
                                if (HttpStatusCode.NotModified == httWebResponse.StatusCode)
                                {
                                    this.NotModified = true;
                                }
                            }
                        }

                        this.ErrorInfo = "下载 " + this.Url + " 过程发生错误: " + ExceptionUtil.GetExceptionMessage(this.e.Error);
                    }
                    else
                    {
                        File.Copy(this.TempFielname, this.OutputFilename, true);
                        try
                        {
                            File.Delete(this.TempFielname);
                        }
                        catch
                        {
                        }
                    }
                }

                this.Close();
            }
                return;
            }
            base.DefWndProc(ref m);
        }