private void ThreadDownload() { //Avoid Memory Leak GC.Collect(); while (LinkURL.Count > 0) { //LoagPage HttpWebClient download = new HttpWebClient(new CookieContainer()); download.Encoding = Encoding.UTF8; byte[] byteArray = download.DownloadData(LinkURL.Dequeue()); String data = Encoding.UTF8.GetString(byteArray); if (data.Contains("Download original") && DownloadOrignChk.Checked) { //假如有大圖切到這裡 String downloadOriginal = data.Substring(data.IndexOf("http://g.e-hentai.org/fullimg.php?")); downloadOriginal = downloadOriginal.Substring(0, downloadOriginal.IndexOf("\"")); downloadOriginal = downloadOriginal.Replace("amp;", ""); //取得Location String str = spwc.DownloadString(downloadOriginal); WebHeaderCollection myWebHeaderCollection = spwc.ResponseHeaders; for (int i = 0; i < myWebHeaderCollection.Count; i++) { if (myWebHeaderCollection.GetKey(i) == "Location") { downloadOriginal = myWebHeaderCollection.Get(i); break; } } if (downloadOriginal.Contains("jpg")) { this.HackListView.Items.Add(new ListViewItem(new String[] { (HackListView.Items.Count + 1).ToString(), downloadOriginal, ".jpg" })); } if (downloadOriginal.Contains("png")) { this.HackListView.Items.Add(new ListViewItem(new String[] { (HackListView.Items.Count + 1).ToString(), downloadOriginal, ".png" })); } if (downloadOriginal.Contains("gif")) { this.HackListView.Items.Add(new ListViewItem(new String[] { (HackListView.Items.Count + 1).ToString(), downloadOriginal, ".gif" })); } } else { //切割圖片網址 Match m; string HRefPattern = "<img id.+?src=[\"'](.+?)[\"'].*?>"; try { m = Regex.Match(data, HRefPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(1)); while (m.Success) { if (m.Groups[1].ToString().Contains("jpg")) { this.HackListView.Items.Add(new ListViewItem(new String[] { (HackListView.Items.Count + 1).ToString(), m.Groups[1].ToString(), ".jpg" })); } if (m.Groups[1].ToString().Contains("png")) { this.HackListView.Items.Add(new ListViewItem(new String[] { (HackListView.Items.Count + 1).ToString(), m.Groups[1].ToString(), ".png" })); } if (m.Groups[1].ToString().Contains("gif")) { this.HackListView.Items.Add(new ListViewItem(new String[] { (HackListView.Items.Count + 1).ToString(), m.Groups[1].ToString(), ".gif" })); } m = m.NextMatch(); } } catch (RegexMatchTimeoutException) { Console.WriteLine("The matching operation timed out."); } } } }
public int loadPagesCount(String url, Object senser) { ((Button)senser).Text = "加載頁面中..."; ((Button)senser).Enabled = false; int pagesCount = 1; byte[] byteArray; String data; try { HttpWebClient title = new HttpWebClient(new CookieContainer()); title.Encoding = Encoding.UTF8; byteArray = title.DownloadData(url); data = Encoding.UTF8.GetString(byteArray); int first = data.IndexOf("<title>"); int last = data.IndexOf("</title>"); donwloadTile = data.Substring(first + 7, last - first - 7); } catch (Exception e) { } //取得下載頁數 Pages.Clear(); Pages.Enqueue(url); try { //check final / char if ((url[url.Length - 1]) != '/') { url += '/'; } //GET NEW SESSION FIX 20160222 ret = spwc.DownloadString(url + "?nw=session", Encoding.UTF8); CookieCollection cookies = cc.GetCookies(new Uri(url)); foreach (Cookie cookie in cookies) { if (cookie.Name == "uconfig") this.webtoken = cookie.Value; } data = spwc.DownloadString(url, Encoding.UTF8); //取得所有p值找最大 GC.Collect(); int maxPages = 0; for (int i = 1000;i >= 0 ; i--) { if (data.IndexOf(url + "?p=" + i) != -1) { maxPages = i; break; } } GC.Collect(); for (int i = 1; i <= maxPages; i++) { pagesCount++; Pages.Enqueue(url + "?p=" + i); } GC.Collect(); StatusNow.Text = "成功獲取資料,頁數:" + pagesCount.ToString() + ",正在Trace中..."; } catch (Exception e) { } finally { newThread = new System.Threading.Thread(this.ThreadProcLoad); newThread.Start(); } return pagesCount; }