`

nodejs 之模拟登录

 
阅读更多

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

登录北邮人的程序:

//**** 是北邮人的用户名 -----是密码

//登录 北邮人论坛
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头部信息

//用户名 : *******
//密码 :------
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();

 

分享到:
评论
1 楼 itway 2016-06-20  
很棒的小demo

相关推荐

Global site tag (gtag.js) - Google Analytics