模拟ASPX页面POST相关参数

来源:互联网 发布:舞蹈服装大全淘宝网 编辑:程序博客网 时间:2024/05/06 08:22

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;

namespace WSSERVER
{
    public partial class Test : Form
    {
        public Test()
        {
            InitializeComponent();
        }

        BZDM dm2 = new BZDM();
        DM dm = new DM();
        private void button1_Click(object sender, EventArgs e)
        {
            //getYL();
          //  getYL();
            string url = "http://61.191.22.155/TYFW/InfoQuery/ZhaBasec.aspx";
            StringBuilder sb=new StringBuilder();
            string mes = getHTMLUTF(url);
              Regex regex = new Regex("(?<=<input type=/"hidden/" name=/"__VIEWSTATE/" id=/"__VIEWSTATE/" value=/")[//s//S]*?(?=/" />)", RegexOptions.IgnoreCase);
            MatchCollection matchs = regex.Matches(mes);
            Regex regex2 = new Regex("(?<=<input type=/"hidden/" name=/"__EVENTVALIDATION/" id=/"__EVENTVALIDATION/" value=/")[//s//S]*?(?=/" />)", RegexOptions.IgnoreCase);
            MatchCollection matchs2 = regex2.Matches(mes);

            //DataSet ds=dm.getsql("select * from FH_SWZ_D WHERE TIME>TO_DATE('" + System.DateTime.Now.ToShortDateString() + "','YYYY-MM-dd') and NAME='"+space+"'");
            sb.Append("__VIEWSTATE=" + convertURL(matchs[0].Value));
            sb.Append("&__EVENTVALIDATION=" + convertURL(matchs2[0].Value));
            sb.Append("&" +  convertURL("DTYearMonthDayHour1$DDLYear") + "=" + System.DateTime.Now.Year);
            sb.Append("&" +  convertURL("DTYearMonthDayHour1$DDLMonth") + "=" + System.DateTime.Now.Month);
            sb.Append("&" +  convertURL("DTYearMonthDayHour1$DDLDay") + "=" + System.DateTime.Now.Day);
            sb.Append("&" + convertURL( "DTYearMonthDayHour1$DDLHour") + "=07");
            sb.Append("&" +  convertURL("DTYearMonthDayHour2$DDLYear") + "=" + System.DateTime.Now.Year);
            sb.Append("&" +  convertURL("DTYearMonthDayHour2$DDLMonth") + "=" + System.DateTime.Now.Month);
            sb.Append("&" +  convertURL("DTYearMonthDayHour2$DDLDay") + "=" + System.DateTime.Now.Day);
            sb.Append("&" +  convertURL("DTYearMonthDayHour2$DDLHour") + "=09");
            sb.Append("&" + "Rads" + "=on");
            sb.Append("&" + "lbxStation" + "=50402500");
            sb.Append("&" + "lbxReturnList" + "=0");
            sb.Append("&" + "TbxList" + "=50402500,");
            sb.Append("&" + "Button1" + "=" + convertURL("查询"));
            string postparam = sb.ToString();
            mes = GetPage(url, postparam);
            Regex regex3 = new Regex("(?<=<tr class=Table112_tr1)[//s//S]*?(?=tr>)", RegexOptions.IgnoreCase);
            MatchCollection matchs3 = regex3.Matches(mes);
            for (int i = 0; i < matchs3.Count; i++)
            {
                string TRSTR = matchs3[i].Value;
                Regex regex5 = new Regex(@"(?<=<td)[/s/S]*?(?=&nbsp;</td>)", RegexOptions.IgnoreCase);
                MatchCollection matchs5 = regex5.Matches(TRSTR);
                string tdstr = matchs5[0].Value;
                string name = tdstr.Split('>')[1];
                tdstr = matchs5[2].Value;
                string sw1 = tdstr.Split('>')[1];
                tdstr = matchs5[4].Value;
                string sw2 = tdstr.Split('>')[1];
                MessageBox.Show(name + ":" + sw1 + "====" + sw2);
            }
           
        
         
        }

        public string convertURL(string tmp)
        {
            return HttpUtility.UrlEncode(tmp);
        }

      

        public string getHTMLGB2312(string URL)
        {
            string aa = "";
            WebRequest req = WebRequest.Create(URL);
            try
            {
                WebResponse result = req.GetResponse();
                //得到的流是网页内容  

                Stream ReceiveStream = result.GetResponseStream();

                StreamReader readerOfStream = new StreamReader(ReceiveStream,

                                System.Text.Encoding.GetEncoding("GB2312"));

                aa = readerOfStream.ReadToEnd();

 

                ReceiveStream.Close();

            }
            catch (Exception)
            {


            }
            return aa;
        }
      

        public string GetPage(string posturl, string postData)
        {
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8");
            byte[] data = encoding.GetBytes(postData);
            // 准备请求...
            try
            {
                // 设置参数
                request = WebRequest.Create(posturl) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                //返回结果网页(html)代码
                string content = sr.ReadToEnd();
                string err = string.Empty;
                return content;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                return string.Empty;
            }
        }


        public string getHTMLUTF(string URL)
        {
            string aa = "";
            WebRequest req = WebRequest.Create(URL);
            try
            {
                WebResponse result = req.GetResponse();
                //得到的流是网页内容  

                Stream ReceiveStream = result.GetResponseStream();

                StreamReader readerOfStream = new StreamReader(ReceiveStream,

                                System.Text.Encoding.GetEncoding("UTF-8"));

                aa = readerOfStream.ReadToEnd();

 

                ReceiveStream.Close();

            }
            catch (Exception)
            {


            }
            return aa;
        }
        public string getHTML(string URL)
        {
            string aa = "";
            WebRequest req = WebRequest.Create(URL);
            try
            {
                WebResponse result = req.GetResponse();
                //得到的流是网页内容  

                Stream ReceiveStream = result.GetResponseStream();

                StreamReader readerOfStream = new StreamReader(ReceiveStream,

                                System.Text.Encoding.GetEncoding("GB2312"));

                aa = readerOfStream.ReadToEnd();

 

                ReceiveStream.Close();

            }
            catch (Exception)
            {


            }
            return aa;
        }
    }
}