nodejs登錄模擬

最近看了 nodejs開發指南這本書,並參照着 nodejs.org官方網站中的 api,寫了一個 模擬登陸程序,其中有 北郵人論壇的,有 163郵箱的。其中前者 只需post就能夠了,竟然是明文傳輸,也不用https。後者選擇了https,經過抓發分析,寫了以下程序,另外,還學習了 經過Nodejs進行web開發,發現 nodejs真的很強大,是作服務器端開發的一款利器。 javascript

登陸北郵人的程序: html

Js代碼    收藏代碼
  1. //**** 是北郵人的用戶名 -----是密碼  
  2.   
  3. //登陸 北郵人論壇  
  4. var http=require("http");  
  5. var querystring=require("querystring");  
  6.   
  7. var contents=querystring.stringify({  
  8.     CookieDate:0,  
  9.     id:"****",  
  10.     mode:0,       
  11.     passwd:"-----"  
  12. });  
  13.   
  14. var options={  
  15.     host:"bbs.byr.cn",  
  16.     path:"/user/ajax_login.json",  
  17.     method:"post",  
  18.     headers:{  
  19.         "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",  
  20.         "Content-Length":contents.length,         
  21.         "Accept":"application/json, text/javascript, */*; q=0.01",  
  22.       
  23.         "Accept-Language":"zh-cn",  
  24.         "Cache-Control":"no-cache",  
  25.         "Connection":"Keep-Alive",    
  26.     ·   "Host":"bbs.byr.cn",  
  27.         "Referer":"http://bbs.byr.cn/index",  
  28.         "User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)",  
  29.         "X-Requested-With":"XMLHttpRequest"  
  30.     }  
  31. };  
  32.   
  33. var req=http.request(options,function(res){  
  34.     res.setEncoding("utf8");  
  35.     res.on("data",function(data){  
  36.         console.log(data);  
  37.     });  
  38. });  
  39.   
  40.   
  41. req.write(contents);  
  42. req.end();  

 登陸163郵箱的程序: java

登陸成功以後,經過抓到的cookie和 跳轉的url,就能夠了,下面打印出了cookie頭部信息 node

Js代碼    收藏代碼
  1. //用戶名 : *******  
  2. //密碼 :------  
  3. var https=require("https");  
  4. var querystring=require("querystring");  
  5. var url="https://ssl.mail.163.com/entry/coremail/fcg/ntesdoor2?"+  
  6.     "df=webmail163&from=web&funcid=loginone&iframe=1&language=-1&net=c&passtype=1&product=mail163&race=-2_60_-2_hz&style=-1&uid=*******@163.com";  
  7.   
  8. var contents=querystring.stringify({  
  9.     savelogin:1,  
  10.     password:"------",  
  11.     url2:"http://mail.163.com/errorpage/err_163.htm",         
  12.     username:"*******"  
  13. });  
  14.   
  15. var options={  
  16.     host:"ssl.mail.163.com",  
  17.     path:"/entry/coremail/fcg/ntesdoor2?df=webmail163&from=web&funcid=loginone&iframe=1&language=-1&net=c&passtype=1&product=mail163&race=-2_60_-2_hz&style=-1&uid=******@163.com",  
  18.     method:"post",  
  19.     headers:{     
  20.         "Content-Type":"application/x-www-form-urlencoded",  
  21.         "Content-Length":contents.length,         
  22.         "Accept":"text/html, application/xhtml+xml, */*",     
  23.         "Accept-Language":"zh-CN",  
  24.         "Cache-Control":"no-cache",  
  25.         "Connection":"Keep-Alive",    
  26.         "Host":"ssl.mail.163.com",  
  27.         "Referer":"http://mail.163.com/",         
  28.         "User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)"  
  29.     }  
  30. };  
  31.   
  32. var req=https.request(options,function(res){      
  33.     res.setEncoding("utf8");  
  34.     var headers=res.headers;  
  35.     //console.log(headers);  
  36.     var cookies=headers["set-cookie"];  
  37.     cookies.forEach(function(cookie){  
  38.         console.log(cookie);  
  39.     });  
  40.     res.on("data",function(data){  
  41.         console.log(data);  
  42.     });  
  43. });  
  44.   
  45. req.write(contents);  
  46. req.end();
相關文章
相關標籤/搜索