nodejs登陆模拟

最近看了 nodejs开发指南这本书,并参照着 nodejs.org官方网站中的 api,写了一个 模拟登录程序,其中有 北邮人论坛的,有 163邮箱的。其中前者 只需post就可以了,居然是明文传输,也不用https。后者选择了https,通过抓发分析,写了如下程序,另外,还学习了 通过Nodejs进行web开发,发现 nodejs真的很强大,是做服务器端开发的一款利器。

登录北邮人的程序:

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邮箱的程序:

登录成功之后,通过抓到的cookie和 跳转的url,就可以了,下面打印出了cookie头部信息

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();

转载于:https://my.oschina.net/u/867090/blog/142477

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值