数据采集之,采集AspNetPager1分页的页面

 private void button2_Click(object sender, EventArgs e)
        {  utility.Eventtarget = "AspNetPager1";  
            utility.Eventargument = "2";
            PrepareData();
            Post();
        }
        public void PrepareData()
        {
             string sHtml = utility. GetPageHTML("url");
            string sPattern = "id=///"__VIEWSTATE///"//svalue=///"(?<ViewState>[^>]*)///"";
            MatchCollection matchs = Regex.Matches(sHtml, sPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            if (matchs.Count == 0) return;
          utility.Viewstate =System.Web.HttpUtility.UrlEncode( matchs[0].Groups["ViewState"].Value);
          
        }
        public void Post()
        {
            string strId = "guest";
            string strPassword = "123456";

            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "__EVENTTARGET=" + utility.Eventtarget;
            //postData += "&__VIEWSTATE=" + utility.Viewstate;
            postData += "&__EVENTARGUMENT=" + utility.Eventargument;
            //string postData = "userid=" + strId;
           // postData += ("&password=" + strPassword);

            byte[] data = encoding.GetBytes(postData);

            // Prepare web request...
            HttpWebRequest myRequest =
            (HttpWebRequest)WebRequest.Create("url");
            myRequest.AllowAutoRedirect = true;
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            Stream newStream = myRequest.GetRequestStream();

            // Send the data.
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            // Get response
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            string content = reader.ReadToEnd();
            //Console.WriteLine(content);
            webBrowser1.DocumentText = content;
            webBrowser1.Refresh();
        }

你可能感兴趣的:(数据采集之,采集AspNetPager1分页的页面)