HQT

追求.NET 技术永无止境

导航


使用 .NET 的 HttpWebRequest 可轻松实现站外提交功能,
代码如下:

            ASCIIEncoding encoding=new ASCIIEncoding();
            
string postData="TextBox1=33&Button1=Button";
            
byte[]  data = encoding.GetBytes(postData);

            
// Prepare web request
            HttpWebRequest myRequest =
                (HttpWebRequest)WebRequest.Create(
"http://localhost/testform1.aspx");
            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();

解释:
postData 为你要提交的数据
比如 CSDN 的登录页面 http://www.csdn.net/member/UserLogin.aspx
输入用户名密码和校验码,并提交之后,浏览器便将下面的数据提交到服务器:
 

CSDNUserLogin%3Atb_UserName=yourName&CSDNUserLogin%3Atb_Password=yourPassword&CSDNUserLogin%3Atb_ExPwd=2332


其中的 yourName 为你实际登陆时提交的用户名, yourPassword 即为你的密码, 2332 是我刚才登陆时的验证码

这里介绍个工具: Visual Sniffer , google 一下便可轻松找到下载地址。
可以使用 Visual Sniffer 来捕捉提交的数据信息:
1. 访问你需要站外提交的页面,比如 CSDN 登陆页 http://www.csdn.net/member/UserLogin.aspx
2. 填写好需要的资料,比如用户名和密码,
3. 打开 Visual Sniffer, 点“开始拦截”
4. 在访问的页面中提交。
5. 等提交成功之后,在 Visual Sniffer 中“停止拦截”
6. 在 Visual Sniffer 的左侧栏的加号中依次点开,右边是它拦截到的内容,
   找到 内容含有 POST  http://www.csdn.net/member/UserLogin.aspx  的节点
以下是我拦截的内容供参考:


POST http://www.csdn.net/member/UserLogin.aspx HTTP/
1.0
Accept: image/gif
, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/
4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)
Host: www.csdn.net
Content-Length: 
355
Proxy-Connection: Keep-Alive
Cookie: ASPSESSIONIDAAAATBQC
=FMEGGCKDBKHAMMCGKPFDMBFG; ASP.NET_SessionId=lusprmnom05lr445tmteaf55; userid=699879

__EVENTTARGET
=&__EVENTARGUMENT=&__VIEWSTATE=dDwtMTcwMzgxNjQ2Mjs7bDxDU0ROVXNlckxvZ2luOmNiX1NhdmVTdGF0ZTtDU0ROVXNlckxvZ2luOkltYWdlX0xvZ2luOz4%2Btu1q2wmRZoAJTi9L73w1zBleylY%3D&CSDNUserLogin%3Atb_UserName=testusername&CSDNUserLogin%3Atb_Password=testpassword&CSDNUserLogin%3Atb_ExPwd=9232&from=&CSDNUserLogin%3AImage_Login.x=36&CSDNUserLogin%3AImage_Login.y=6
GET http://www.csdn.net/mycustompage.htm?aspxerrorpath
=/member/UserLogin.aspx HTTP/1.0
Accept: image/gif
, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/
4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)
Host: www.csdn.net
Proxy-Connection: Keep-Alive
Cookie: ASPSESSIONIDAAAATBQC
=FMEGGCKDBKHAMMCGKPFDMBFG; ASP.NET_SessionId=lusprmnom05lr445tmteaf55; userid=699879


 

 



注意:PostData 参数之间是以 " & " 进行 连接的

OK,通过以上简单示例,只要稍微修改下,即可做成多站点自动登陆,或自动网上投票等功能!

参考网址:

http://dev.csdn.net/article/28/28374.shtm
http://www.knowsky.com/18774.html
http://www.netomatix.com/HttpPostData.aspx