using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Text.RegularExpressions;using System.Collections;using System.Collections.Generic;using System.Net;using System.IO;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { InitData(); } } protected void btnCollect_Click(object sender, EventArgs e) { string strUrl = txtCollectionUrl.Text.Trim(); List urls = new List (); List serialsStrings = new List (); Dictionary dic = new Dictionary (); int iStart = 0; int iEnd = 0; if (string.IsNullOrEmpty(strUrl)) return; string hasPattern = @"{*}"; Regex reg = new Regex(hasPattern, RegexOptions.Compiled | RegexOptions.Singleline); if (reg.IsMatch(strUrl)) { int.TryParse(txtStart.Text.Trim(), out iStart); int.TryParse(txtEnd.Text.Trim(), out iEnd); serialsStrings = GetSerialStrings(iStart, iEnd, Convert.ToInt32(ddlSimilarCount.SelectedValue)); foreach (string var in serialsStrings) { urls.Add(strUrl.Replace(hasPattern,var)); } } else { urls.Add(strUrl); } foreach (string var in urls) { Response.Write(var + " "); } foreach (string var in urls) { WebRequest request = WebRequest.Create(var); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default); string strContent = reader.ReadToEnd(); string strContentPattern = txtPrefixRule.Text + @".*?" + txtSuffixRule.Text; Regex regContent = new Regex(strContentPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); MatchCollection mc = regContent.Matches(strContent); foreach (Match match in mc) { //Response.Write(match.Value); dic["Issue"] = GetContent(match.Value, txtPrefixIssueRule.Text, txtSubffixIssueRule.Text); dic["Lottery"] = GetContent(match.Value, txtPrefixLotteryRule.Text, txtSubffixLotteryRule.Text); dic["Result"] = GetContent(match.Value, txtPrefixResultRule.Text, txtSubffixResultRule.Text); dic["SaleRoom"] = GetContent(match.Value, txtPrefixSaleRoomRule.Text, txtSubffixSaleRoomRule.Text); dic["Date"] = GetContent(match.Value, txtPrefixDateRule.Text, txtSubffixDateRule.Text); } } foreach (object var in collection_to_loop) { } } private string GetContent(string content, string startStr, string endStr) { string result = string.Empty; int indexA = content.IndexOf(startStr); int indexB = content.IndexOf(endStr); if (indexA > -1 && indexB > -1) { if (indexA > indexB) { indexB = content.IndexOf(endStr, indexA); //indexA = 0; } if (indexB > -1) result = content.Substring(indexA + startStr.Length, indexB - indexA - startStr.Length); } return result; } private void InitData() { this.txtStart.Text = "0"; this.txtEnd.Text = "0"; for (int i = 1; i <= 5; i++) { ddlSimilarCount.Items.Add(new ListItem(i.ToString(), i.ToString())); } ddlSimilarCount.SelectedValue = "2"; } private List GetSerialStrings(int iStart, int iEnd, int length) { int iTempStart = iStart; int iTempEnd = iEnd; List serialStrings = new List (); if (iStart > iEnd) { iTempStart = iEnd; iTempEnd = iStart; } if (iTempEnd.ToString().Length > length) length = iTempEnd.ToString().Length; int iCount = iTempEnd - iTempStart; for (int i = iTempStart; i <= iTempEnd; i++) { serialStrings.Add(i.ToString().PadLeft(length, '0')); } return serialStrings; }}