使用 HttpWebRequest 輕鬆實現站外提交(可用於自動登陸,自動網上投票等) (轉)

使用 HttpWebRequest 輕鬆實現站外提交(可用於自動登陸,自動網上投票等)
Posted on 2005-10-08 15:13 HQT 閱讀(780) 評論(1)  編輯 收藏 收藏至365Key 所屬分類: .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
 
 
發佈了29 篇原創文章 · 獲贊 1 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章