public void Download(int from, int to, string sid, string qid) { CookieCollection cookies = new CookieCollection(); Downloader downloader = new Downloader(sid, qid, cookies); if (to - from + 1 > 500) { for (int i = from; i < to+1; i += 500) { if (i + 500> to) { SaveFile(downloader, to - from + 1, i, to); } else { SaveFile(downloader, to - from + 1, i, i + 499); } } } }
/// <summary> /// 保存文件 /// </summary> /// <param name="downloader"></param> /// <param name="total">需要保存的文件总数</param> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> private bool SaveFile(Downloader downloader, int total, int from, int to) { string filepath = Path.Combine(tbxFolder.Text.Trim(), GetFileName(from, to)); try { using (StreamWriter writer = new StreamWriter(filepath, false, Encoding.Default)) { writer.Write(downloader.DownloadData(from, to)); writer.Flush(); writer.Close(); } AddCurrentCount(to - from + 1); Func<bool> report = () => { ReportProcess(); return true; }; this.Invoke(report); return true; } catch (IOException) { return false; } }
public void DownloadFiles() { Item item = null; Func<bool> report = () => { Report(); return true; }; Func<string, bool> addMessage = (message) => { AddMessage(message); return true; }; while ((item = GetItemFromQueue()) != null) { CookieCollection cookies = new CookieCollection(); Downloader downloader = new Downloader(Sid, Qid, cookies); if (DownloadFile(downloader, item)) { //文件报告 AddCurrentCount(item.Count); this.Invoke(report); this.Invoke(addMessage, string.Format("[{0}]:{1}-{2}-{3}.txt 文件下载完成", DateTime.Now, item.From, item.To, item.Count)); } else { AddQueue(item); //文件报告 } } }
void Download() { int from = 0; int to = 0; int total = 0; int.TryParse(tbxFrom.Text, out from); int.TryParse(tbxTo.Text, out to); int.TryParse(tbxTotalCount.Text, out total); if (to - from + 1 <= total && from <= total && to <= total && from <= to) { TotalCount = to - from + 1; if (to - from + 1 > 500) { Task task = new Task(() => { DownloadAll(from, to, tbxSID.Text.Trim(), tbxQueryid.Text.Trim()); }); task.Start(); Task.WaitAll(task); AddLog("下载完成"); } else { CookieCollection cookies = new CookieCollection(); Downloader downloader = new Downloader(tbxSID.Text, tbxQueryid.Text, cookies); SaveFile(downloader, to - from + 1, from, to); AddLog("下载完成"); } } }
public bool DownloadFile(Downloader downloader, Item item) { string filepath = Path.Combine(tbxFolder.Text.Trim(), GetFileName(item)); try { using (StreamWriter writer = new StreamWriter(filepath, false, Encoding.Default)) { downloader.PostContent = outbountserviceString; writer.Write(downloader.DownloadData(item.From, item.To)); writer.Flush(); writer.Close(); System.Threading.Thread.Sleep(ThreadSleepTime); } return true; } catch (IOException) { return false; } catch (WebException) { return false; } }