实现网站的自动登录

首先打开网站,查看源文件,找到他的登录表单部分。现使用的表单如下:
<form name="Form1" method="get" action="http://market.p5w.net/kfsjj/Default.aspx" id="Form1">
                        <input type="hidden" name="__VIEWSTATE" value="dDwxMDExMDg1MDE5Ozs+7oCNLtscnBugMKq9iGi++HHIFAQ=1" />
            <input name="textBoxRq" type="text" value="2007-7-27" id="textBoxRq" style="width:97px;" />   
            <input name="textBoxZh" type="text" value="519519"    id="textBoxZh"  />
            <input name="textBoxMm" type="password" value="519519"  id="textBoxMm" />
                        <input type="hidden" name="btnLogin" value="登陆" />
</form>
以上__VIEWSTATE,textBoxRq,textBoxZh,textBoxMm,btnLogin五个字段缺一不可(当然若果少几个,代码编写更容易)。代码如下:
             string name = "519519";
            string pwd = "519519";
            string year = dateTimePicker1.Value.Year.ToString();、、dateTimePicker1为时间控件
            string month = dateTimePicker1.Value.Month.ToString();
            string day = dateTimePicker1.Value.Day.ToString();

            string login = "登陆";
            login = System.Web.HttpUtility.UrlEncode(login);//shiyong 网页地址格式的编码

            CookieContainer cc = new CookieContainer();//缓存

            string FormURL = urlstr;          //处理表单的绝对URL地址
            string FormData = "__VIEWSTATE=" + viestatevalue + "&textBoxRq=" + year + "-" + month + "-" + day + "&textBoxZh=" + name + "&textBoxMm=" + pwd + "&btnLogin=" + login;    //表单需要提交的参数,
                                                                                           //__VIEWSTATE值存在于网页源文件里用正则表达式抓取

            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(FormData);//二进制格式编码

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
            request.Method = "POST";    //数据提交方式
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
            //模拟一个UserAgent
            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            request.CookieContainer = cc;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            cc.Add(response.Cookies);
            Stream stream = response.GetResponseStream();
            string WebContent = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
           
            HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(LaterURL);
            Myrequest.CookieContainer = cc;
            HttpWebResponse Myresponse = (HttpWebResponse)Myrequest.GetResponse();
            cc.Add(Myresponse.Cookies);
            Stream Mystream = Myresponse.GetResponseStream();
            string strResult = new StreamReader(Mystream, System.Text.Encoding.UTF8).ReadToEnd();
                                                                              //strResult为网页源文件字符串
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值