示例#1
0
        async public Task <bool> DownloadAsync()
        {
            var ts = DateTime.Now - this.lastCheckTime;

            if (ts.TotalSeconds < this.intervalSeconds)
            {
                return(false);
            }

            bool   isChanged = false;
            string oldMD5    = this.MD5;

            this.checkCounter++;
            this.lastCheckTime = DateTime.Now;
            Stopwatch sw    = Stopwatch.StartNew();
            Exception error = null;

            using (var httpClient = new HttpClient())
            {
                try
                {
                    HttpResponseMessage respone = await httpClient.GetAsync(DownloadUri);

                    respone.EnsureSuccessStatusCode();
                    HttpContent         httpContent     = respone.Content;
                    HttpContentHeaders  contentHeaders  = httpContent.Headers;
                    HttpResponseHeaders responseHeaders = respone.Headers;

                    if (this.contentLength != contentHeaders.ContentLength)
                    {
                        isChanged = true;
                    }
                    else if (contentLastModified != ToDateTime(contentHeaders.LastModified))
                    {
                        isChanged = true;
                    }
                    this.contentLength       = contentHeaders.ContentLength;
                    this.contentLastModified = ToDateTime(contentHeaders.LastModified);
                    this.contentType         = contentHeaders.ContentType;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        if (IsNeedCheckMD5)
                        {
                            await httpContent.CopyToAsync(ms);

                            string newMD5 = MD5Code.StreamMD5(ms);
                            if (this.md5 != newMD5)
                            {
                                this.md5  = newMD5;
                                isChanged = true;
                            }
                        }//IsNeedCheckMD5

                        if (isChanged)
                        {
                            this.lastChangeTime = DateTime.Now;
                            changeCounter++;

                            if (ms.Length > 0) //已经下载过
                            {
                                if (IsNeedCheck_PT_SS)
                                {
                                    this.pt_ss = Search_PT_SS(ms);

                                    /**
                                     * if (this.pt_ss != ptss)
                                     * {
                                     *
                                     *  this.pt_ss = ptss;
                                     * }
                                     * **/
                                }//IsNeedCheck_PT_SS
                                ms.Position = 0;
                                using (FileStream fs = File.Create(tempFileName))
                                {
                                    ms.Position = 0;
                                    ms.WriteTo(fs);
                                }
                            }    //(ms.Length > 0)
                            else //
                            {
                                using (FileStream fs = File.Create(tempFileName))
                                {
                                    await httpContent.CopyToAsync(fs);
                                }
                                //long len = await transfer.DownloadAsync(new Uri(this.Uri), this.SaveFileName, httpClient);
                            }
                        }//(isChanged)

                        errorCounter = 0;
                    }// using (MemoryStream ms
                }
                catch (Exception ex)
                {
                    error = ex;

                    errorCounter++;
                    if (errorCounter > 20)
                    {
                        errorCounter = 20;
                    }
                    errorMsg           = ex.Message;
                    this.lastCheckTime = DateTime.Now.AddSeconds(errorCounter * 60); //推迟下次请求时间
                    //loger.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
                }
                //NotifyResult(transfer, downloadNotify, isChanged, error); //update by hua
                //this.NotifyResultAsync(transfer, downloadNotify, isChanged, error);
                if (isChanged)
                {
                    foreach (var dsf in SaveFiles.Values)
                    {
                        await dsf.FromAsync(this.tempFileName);
                    }
                }
                sw.Stop();
                this.procDuration = (long)sw.Elapsed.TotalMilliseconds;

                return(isChanged);
            }//using httpclient
        }