/// <summary> /// 开始下载 /// </summary> public void Start() { try { Downloading = true; Stoped = false; HttpWebRequest Request = WebRequest.Create(Url) as HttpWebRequest; HttpWebResponse Response = Request.GetResponse() as HttpWebResponse; if (!File.Exists(DownloadPath + ".dcj")) { DownloadInfo info = new DownloadInfo { ContentLength = Response.ContentLength, BlockLength = Response.ContentLength / ThreadNum, DownloadUrl = Url }; info.init(DownloadPath + ".dcj"); } Info = JsonConvert.DeserializeObject <DownloadInfo>(File.ReadAllText(DownloadPath + ".dcj")); if (Info.Completed) { Downloading = false; Completed = true; Percentage = 100F; return; } if (!File.Exists(DownloadPath)) { FileStream Stream = new FileStream(DownloadPath, FileMode.CreateNew); Stream.SetLength(Response.ContentLength); Stream.Close(); } Console.WriteLine(Info.DownloadBlockList.Count); Threads = new DownloadThread[Info.DownloadBlockList.Count]; for (int i = 0; i < Info.DownloadBlockList.Count; i++) { DownloadBlock Block = JsonConvert.DeserializeObject <DownloadBlock>(Info.DownloadBlockList[i].ToString()); Threads[i] = new DownloadThread { ID = i, DownloadUrl = Url, Path = DownloadPath, Block = Block, Info = Info }; Threads[i].ThreadCompletedEvent += HttpDownload_ThreadCompletedEvent; } new Thread(a).Start(); } catch (Exception ex) { Downloading = false; Stoped = true; Console.WriteLine("出现错误: " + ex.ToString()); } }
public void CreateDataFile() { try { HttpWebRequest Request = WebRequest.Create(Url) as HttpWebRequest; Request.Referer = "http://pan.baidu.com/disk/home"; if (Cookies != null) { Cookie ck = new Cookie("BDUSS", Cookies.BDUSS); ck.Domain = ".baidu.com"; Request.CookieContainer = new CookieContainer(); Request.CookieContainer.Add(ck); ck = new Cookie("pcsett", Cookies.PCSETT); ck.Domain = ".baidu.com"; Request.CookieContainer.Add(ck); } HttpWebResponse Response = Request.GetResponse() as HttpWebResponse; if (!File.Exists(DownloadPath + ".dcj")) { LogTool.WriteLogDebug(typeof(HttpDownload), "正在创建文件: " + DownloadPath + ".dcj"); DownloadInfo info = new DownloadInfo { ContentLength = Response.ContentLength, BlockLength = Response.ContentLength / ThreadNum, DownloadUrl = Url, Cookies = Cookies }; info.init(DownloadPath + ".dcj"); } Info = JsonConvert.DeserializeObject <DownloadInfo>(File.ReadAllText(DownloadPath + ".dcj")); } catch (Exception ex) { LogTool.WriteLogError(typeof(HttpDownload), "创建数据文件出现错误", ex); MessageBox.Show("创建数据文件时出现错误: " + ex.Message); File.Delete(DownloadPath + ".dcj"); } }
public void CreateDataFile() { try { HttpWebRequest Request = WebRequest.Create(Url) as HttpWebRequest; HttpWebResponse Response = Request.GetResponse() as HttpWebResponse; if (!File.Exists(DownloadPath + ".dcj")) { DownloadInfo info = new DownloadInfo { ContentLength = Response.ContentLength, BlockLength = Response.ContentLength / ThreadNum, DownloadUrl = Url }; info.init(DownloadPath + ".dcj"); } Info = JsonConvert.DeserializeObject <DownloadInfo>(File.ReadAllText(DownloadPath + ".dcj")); } catch (Exception ex) { MessageBox.Show("创建数据文件时出现错误: " + ex.Message); File.Delete(DownloadPath + ".dcj"); } }
/// <summary> /// 开始下载 /// </summary> public void Start() { try { Downloading = true; Stoped = false; HttpWebRequest Request = WebRequest.Create(Url) as HttpWebRequest; Request.Referer = "http://pan.baidu.com/disk/home"; //第一次下载设置Cookies if (Cookies != null) { Cookie ck = new Cookie("BDUSS", Cookies.BDUSS); ck.Domain = ".baidu.com"; Request.CookieContainer = new CookieContainer(); Request.CookieContainer.Add(ck); ck = new Cookie("pcsett", Cookies.PCSETT); ck.Domain = ".baidu.com"; Request.CookieContainer.Add(ck); } //改为获取Response之前读入数据文件,这样就能读取到Cookies了 if (File.Exists(DownloadPath + ".dcj")) { Info = JsonConvert.DeserializeObject <DownloadInfo>(File.ReadAllText(DownloadPath + ".dcj")); 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); } } HttpWebResponse Response = Request.GetResponse() as HttpWebResponse; if (!File.Exists(DownloadPath + ".dcj")) { Info = new DownloadInfo { ContentLength = Response.ContentLength, BlockLength = Response.ContentLength / ThreadNum, DownloadUrl = Url, Cookies = Cookies }; Info.init(DownloadPath + ".dcj"); } if (Info.Completed) { Downloading = false; Completed = true; Percentage = 100F; return; } if (!File.Exists(DownloadPath)) { LogTool.WriteLogDebug(typeof(HttpDownload), "正在创建文件: " + DownloadPath); FileStream Stream = new FileStream(DownloadPath, FileMode.CreateNew); Stream.SetLength(Response.ContentLength); Stream.Close(); } Threads = new DownloadThread[Info.DownloadBlockList.Count]; for (int i = 0; i < Info.DownloadBlockList.Count; i++) { DownloadBlock Block = JsonConvert.DeserializeObject <DownloadBlock>(Info.DownloadBlockList[i].ToString()); Threads[i] = new DownloadThread { ID = i, DownloadUrl = Url, Path = DownloadPath, Block = Block, Info = Info }; Threads[i].ThreadCompletedEvent += HttpDownload_ThreadCompletedEvent; } new Thread(a).Start(); } catch (Exception ex) { Downloading = false; Stoped = true; LogTool.WriteLogError(typeof(HttpDownload), "创建下载任务出现错误", ex); } }