生活随笔
收集整理的這篇文章主要介紹了
nodejs登陆模拟
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
nodejs 之模擬登錄
最近看了 nodejs開發指南這本書,并參照著 nodejs.org官方網站中的 api,寫了一個 模擬登錄程序,其中有 北郵人論壇的,有 163郵箱的。其中前者 只需post就可以了,居然是明文傳輸,也不用https。后者選擇了https,通過抓發分析,寫了如下程序,另外,還學習了 通過Nodejs進行web開發,發現 nodejs真的很強大,是做服務器端開發的一款利器。
登錄北郵人的程序:
Js代碼? ?
//****?是北郵人的用戶名?-----是密碼?? ?? //登錄?北郵人論壇?? var?http=require("http");?? var?querystring=require("querystring");?? ?? var?contents=querystring.stringify({?? ????CookieDate:0,?? ????id:"****",?? ????mode:0,??????? ????passwd:"-----"?? });?? ?? var?options={?? ????host:"bbs.byr.cn",?? ????path:"/user/ajax_login.json",?? ????method:"post",?? ????headers:{?? ????????"Content-Type":"application/x-www-form-urlencoded;?charset=UTF-8",?? ????????"Content-Length":contents.length,????????? ????????"Accept":"application/json,?text/javascript,?*/*;?q=0.01",?? ?????? ????????"Accept-Language":"zh-cn",?? ????????"Cache-Control":"no-cache",?? ????????"Connection":"Keep-Alive",???? ????·???"Host":"bbs.byr.cn",?? ????????"Referer":"http://bbs.byr.cn/index",?? ????????"User-Agent":"Mozilla/5.0?(compatible;?MSIE?9.0;?Windows?NT?6.1;?Trident/5.0;?BOIE9;ZHCN)",?? ????????"X-Requested-With":"XMLHttpRequest"?? ????}?? };?? ?? var?req=http.request(options,function(res){?? ????res.setEncoding("utf8");?? ????res.on("data",function(data){?? ????????console.log(data);?? ????});?? });?? ?? ?? req.write(contents);?? req.end();?? ?登錄163郵箱的程序:
登錄成功之后,通過抓到的cookie和 跳轉的url,就可以了,下面打印出了cookie頭部信息
Js代碼? ?
//用戶名?:?*******?? //密碼?:------?? var?https=require("https");?? var?querystring=require("querystring");?? var?url="https://ssl.mail.163.com/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";?? ?? var?contents=querystring.stringify({?? ????savelogin:1,?? ????password:"------",?? ????url2:"http://mail.163.com/errorpage/err_163.htm",????????? ????username:"*******"?? });?? ?? var?options={?? ????host:"ssl.mail.163.com",?? ????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",?? ????method:"post",?? ????headers:{????? ????????"Content-Type":"application/x-www-form-urlencoded",?? ????????"Content-Length":contents.length,????????? ????????"Accept":"text/html,?application/xhtml+xml,?*/*",????? ????????"Accept-Language":"zh-CN",?? ????????"Cache-Control":"no-cache",?? ????????"Connection":"Keep-Alive",???? ????????"Host":"ssl.mail.163.com",?? ????????"Referer":"http://mail.163.com/",????????? ????????"User-Agent":"Mozilla/5.0?(compatible;?MSIE?9.0;?Windows?NT?6.1;?Trident/5.0;?BOIE9;ZHCN)"?? ????}?? };?? ?? var?req=https.request(options,function(res){?????? ????res.setEncoding("utf8");?? ????var?headers=res.headers;?? ????//console.log(headers);?? ????var?cookies=headers["set-cookie"];?? ????cookies.forEach(function(cookie){?? ????????console.log(cookie);?? ????});?? ????res.on("data",function(data){?? ????????console.log(data);?? ????});?? });?? ?? req.write(contents);?? req.end();
轉載于:https://my.oschina.net/u/867090/blog/142477
總結
以上是生活随笔為你收集整理的nodejs登陆模拟的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。