模拟ASPX页面POST相关参数

该博客演示了如何使用C#模拟POST请求访问ASPX页面,通过正则表达式提取隐藏字段,设置请求参数,然后获取并解析响应内容,展示如何处理网页数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值