示例#1
0
 public void Start()
 {
     try
     {
         Thread.Sleep(1000);
         if (Block.Completed)
         {
             ThreadCompletedEvent?.Invoke();
             return;
         }
         Request         = WebRequest.Create(DownloadUrl) as HttpWebRequest;
         Request.Timeout = 5000;
         Request.AddRange(Block.From, Block.To);
         Response = Request.GetResponse() as HttpWebResponse;
         if (!File.Exists(Path))
         {
             Console.WriteLine("出现错误: 本地数据文件不存在");
             return;
         }
         using (Stream ResponseStream = Response.GetResponseStream())
         {
             using (FileStream Stream = new FileStream(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
             {
                 Stream.Seek(Block.From, SeekOrigin.Begin);
                 byte[] Array = new byte[4096];
                 int    i     = ResponseStream.Read(Array, 0, Array.Length);
                 while (i > 0)
                 {
                     Stream.Write(Array, 0, i);
                     Block.From                += i;
                     Block.CompletedLength     += i;
                     Info.CompletedLength      += i;
                     Info.DownloadBlockList[ID] = Block;
                     i = ResponseStream.Read(Array, 0, Array.Length);
                 }
                 Block.Completed = true;
                 ThreadCompletedEvent?.Invoke();
             }
         }
     }
     catch (Exception ex)
     {
         if (ex is ThreadAbortException)
         {
             return;
         }
         if (ex.Message.Contains("终止") || ex.Message.Contains("取消"))
         {
             return;
         }
         if (num < 5)
         {
             num++;
             Console.WriteLine("出现错误: " + ex.ToString() + "正在重试 次数" + num);
             Start();
         }
         Console.WriteLine("出现错误: " + ex.ToString());
     }
 }
示例#2
0
 public void Start()
 {
     try
     {
         Thread.Sleep(1000);
         if (Block.Completed)
         {
             ThreadCompletedEvent?.Invoke();
             return;
         }
         Request           = WebRequest.Create(DownloadUrl) as HttpWebRequest;
         Request.UserAgent = "netdisk;5.3.4.5;PC;PC-Windows;5.1.2600;WindowsBaiduYunGuanJia";
         Request.Referer   = "http://pan.baidu.com/disk/home";
         if (Info.Cookies != null)
         {
             Cookie ck = new Cookie("BDUSS", Info.Cookies.BDUSS);
             ck.Domain = ".baidu.com";
             Request.CookieContainer = new CookieContainer();
             Request.CookieContainer.Add(ck);
             ck        = new Cookie("pcsett", Info.Cookies.PCSETT);
             ck.Domain = ".baidu.com";
             Request.CookieContainer.Add(ck);
         }
         Request.Timeout = 10000;
         Request.AddRange(Block.From, Block.To);
         Response = Request.GetResponse() as HttpWebResponse;
         if (!File.Exists(Path))
         {
             LogTool.WriteLogInfo(typeof(DownloadThread), "下载线程出现错误: 数据文件不存在");
             return;
         }
         using (Stream ResponseStream = Response.GetResponseStream())
         {
             using (FileStream Stream = new FileStream(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
             {
                 Stream.Seek(Block.From, SeekOrigin.Begin);
                 byte[] Array = new byte[4096];
                 int    i     = ResponseStream.Read(Array, 0, Array.Length);
                 while (true)
                 {
                     if (i <= 0 && Block.From - 1 != Block.To && Block.From != Block.To)
                     {
                         //发送空数据,放弃这个链接重试
                         WorkThread = new Thread(Start);
                         WorkThread.Start();
                         return;
                     }
                     if (i <= 0)
                     {
                         break;
                     }
                     Stream.Write(Array, 0, i);
                     Block.From                += i;
                     Block.CompletedLength     += i;
                     Info.CompletedLength      += i;
                     Info.DownloadBlockList[ID] = Block;
                     i = ResponseStream.Read(Array, 0, Array.Length);
                 }
                 Block.Completed = true;
                 ThreadCompletedEvent?.Invoke();
             }
         }
     }
     catch (Exception ex)
     {
         if (ex is ThreadAbortException)
         {
             return;
         }
         if (ex.Message.Contains("终止") || ex.Message.Contains("取消"))
         {
             return;
         }
         if (num < 5)
         {
             //num++;
             //LogTool.WriteLogError(typeof(DownloadThread),"下载线程出现错误,重试中,次数: "+num,ex);
             WorkThread = new Thread(Start);
             WorkThread.Start();
             return;
         }
         LogTool.WriteLogError(typeof(DownloadThread), "下载线程出现错误,重试次数超过阈值,放弃重试", ex);
     }
 }
 public void Start()
 {
     try
     {
         Thread.Sleep(300);
         _request?.Abort();
         _response?.Close();
         if (_stoped)
         {
             return;
         }
         if (Block.Completed)
         {
             ThreadCompletedEvent?.Invoke();
             return;
         }
         _request           = WebRequest.Create(DownloadUrl) as HttpWebRequest;
         _request.UserAgent = Info.UserAgent;
         _request.Referer   = Info.Referer;
         if (Info.UserCookies != null)
         {
             _request.CookieContainer = new CookieContainer();
             foreach (var Key in Info.UserCookies.GetKeys())
             {
                 var ck = new Cookie(Key, Info.UserCookies.GetCookie(Key))
                 {
                     Domain = Info.Domain
                 };
                 _request.CookieContainer.Add(ck);
             }
         }
         _request.Timeout = 8000;
         _request.AddRange(Block.From, Block.To);
         _response = _request.GetResponse() as HttpWebResponse;
         if (!File.Exists(Path))
         {
             return;
         }
         using (var responseStream = _response.GetResponseStream())
         {
             using (var stream = new FileStream(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 1024 * 1024))
             {
                 stream.Seek(Block.From, SeekOrigin.Begin);
                 var array = new byte[1024];
                 var i     = responseStream.Read(array, 0, array.Length);
                 while (true)
                 {
                     if (i <= 0 && Block.From - 1 != Block.To && Block.From != Block.To)
                     {
                         //发送空数据,放弃这个链接重试
                         _workThread = new Thread(Start);
                         _workThread.Start();
                         return;
                     }
                     if (i <= 0)
                     {
                         break;
                     }
                     stream.Write(array, 0, i);
                     Block.From                += i;
                     Block.CompletedLength     += i;
                     Info.CompletedLength      += i;
                     Info.DownloadBlockList[ID] = Block;
                     i = responseStream.Read(array, 0, array.Length);
                 }
                 Block.Completed = true;
                 ThreadCompletedEvent?.Invoke();
             }
         }
     }
     catch (Exception ex)
     {
         if (ex is ThreadAbortException)
         {
             return;
         }
         if (ex.Message.Contains("终止") || ex.Message.Contains("取消"))
         {
             return;
         }
         Next();
         _workThread = new Thread(Start);
         _workThread.Start();
     }
 }
示例#4
0
        public void Start()
        {
            try
            {
                Thread.Sleep(300);
                _request?.Abort();
                _response?.Close();

                if (_stoped)
                {
                    return;
                }

                if (Block.IsCompleted)
                {
                    ThreadCompletedEvent?.Invoke(Path);
                    return;
                }

                _request                  = WebRequest.Create(DownloadUrl) as HttpWebRequest;
                _request.SendChunked      = false;
                _request.KeepAlive        = false;
                _request.ReadWriteTimeout = 2000;
                _request.Timeout          = 2000;
                _request.AddRange("bytes", Block.From, Block.To);


                _response = _request.GetResponse() as HttpWebResponse;

                if (!File.Exists(Path))
                {
                    return;
                }

                using (var responseStream = _response.GetResponseStream())
                {
                    using (var stream = new FileStream(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 1024 * 1024))
                    {
                        stream.Seek(Block.From, SeekOrigin.Begin);
                        var array = new byte[1024];
                        var i     = responseStream.Read(array, 0, array.Length);
                        while (true)
                        {
                            if (i <= 0 && Block.From - 1 != Block.To && Block.From != Block.To)
                            {
                                //发送空数据,放弃这个链接重试
                                _workThread = new Thread(Start)
                                {
                                    IsBackground = true
                                };
                                _workThread.Start();
                                return;
                            }
                            if (i <= 0)
                            {
                                break;
                            }
                            stream.Write(array, 0, i);
                            Block.From                += i;
                            Block.CompletedLength     += i;
                            Info.CompletedLength      += i;
                            Info.DownloadBlockList[ID] = Block;

                            i = responseStream.Read(array, 0, array.Length);
                        }
                        Block.IsCompleted = true;
                        ThreadCompletedEvent?.Invoke(Path);
                    }
                }


                _response?.Close();
                _request?.Abort();
            }
            catch (Exception ex)
            {
                GC.Collect();

                if (ex.Message.Contains("操作"))
                {
                    Debug.WriteLine(ex.StackTrace);

                    //NextUrl();
                    //goto Retry;
                }


                if (ex is ThreadAbortException)
                {
                    return;
                }

                if (ex.Message.Contains("终止") || ex.Message.Contains("取消"))
                {
                    return;
                }



                NextUrl();
                _workThread = new Thread(Start)
                {
                    IsBackground = true
                };
                _workThread.Start();
            }
        }
 private void Start()
 {
     //await Task.Delay(300);
     Thread.Sleep(300);
     while (true)
     {
         try
         {
             _request?.Abort();
             _response?.Close();
             if (_stoped)
             {
                 return;
             }
             if (Block.Completed)
             {
                 ThreadCompletedEvent?.Invoke();
                 return;
             }
             if (Block.From > Block.To)
             {
                 ThreadCompletedEvent?.Invoke();
                 return;
             }
             _request           = WebRequest.Create(DownloadUrl) as HttpWebRequest;
             _request.UserAgent = Info.UserAgent;
             _request.Referer   = Info.Referer;
             if (Info.UserCookies != null)
             {
                 _request.CookieContainer = new CookieContainer();
                 foreach (var Key in Info.UserCookies.GetKeys())
                 {
                     var ck = new Cookie(Key, Info.UserCookies.GetCookie(Key))
                     {
                         Domain = Info.Domain
                     };
                     _request.CookieContainer.Add(ck);
                 }
             }
             _request.Timeout          = 8000;
             _request.ReadWriteTimeout = 3000;
             _request.AddRange(Block.From, Block.To);
             _response = _request.GetResponse() as HttpWebResponse;
             if (!File.Exists(Path))
             {
                 return;
             }
             using (var responseStream = _response.GetResponseStream())
             {
                 using (var stream = new FileStream(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 1024 * 1024))
                 {
                     stream.Seek(Block.From, SeekOrigin.Begin);
                     var array = new byte[1024];
                     var i     = responseStream.Read(array, 0, array.Length);
                     for (var j = 0; j < 25; j++)
                     {
                         if (_stoped)
                         {
                             return;
                         }
                         if (i <= 0 && Block.From - 1 != Block.To && Block.From != Block.To)
                         {
                             continue;
                         }
                         if (i <= 0)
                         {
                             Block.Completed = true;
                             break;
                         }
                         stream.Write(array, 0, i);
                         Block.From                += i;
                         Block.CompletedLength     += i;
                         Info.CompletedLength      += i;
                         Info.DownloadBlockList[ID] = Block;
                         i = responseStream.Read(array, 0, array.Length);
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine("Error: " + ex);
         }
     }
 }