想用node写一个模拟登录人人之类的程序碰到的几个问题:
- 当返回状态码是302时要怎么弄,要新开一个链接吗?
- 人人貌似使用cookie来检测账号,怎么把cookie存到本地
5 回复
- 302转向:
res.statusCode = 302;
res.setHeader('location', '要跳转到的网址');
res.end();
浏览器在收到这个响应之后,会自动调整到location响应头中指定的网址
使用express示例:res.redirect('要调整到的网址', 302);
- 设置Cookie:
res.setHeader('cookie', 'cookie内容');
浏览器在收到响应之后,会将该Cookie保存到本地
使用express示例:res.cookie('名称', '值', {maxAge: 3600000});
- 不明白你所说的模拟登录是指要实现一个类似人人网那样的登录功能,还是你要通过你的程序登录到人人网。
@nobuta 参考这里:http://cnodejs.org/topic/504f7c501f186ae131021980 看后面的回复
302跳转示例:
var http = require('http');
var url = require('url');
http.get({
host:"www.google.com.hk",
port:80,
path:"/search?tbs=sbi:AMhZZivK0ZITunLa61jpSnTUgxTzXokI3SvM61VJdh8_1jtioUzo-LZMimgb-a3eh0eu6CtIrSOMldhyYsbBsK3H_1PaFE1c6LA4FeeeqH3ZLSO6ocA9tUmVU-XD2IsWQL91FcogEj85LxW_1D9JD62lt6jYMRYyXJAFgws_1BIuWBztJ27pNSGd-2YHGKVPQCOMubdQ9Cs86bYBsKaA1FsyM7MQDqr4WZ9sNeH_1ZLUcbDUTuWTQ25khYsCYmpX2XYDeMGtj_1xwZtlrrMDRINTYDKlCfAKy9CgqYClVWZk9xJSRNDcsyfbUk9rJIY2whiJ70B4e3J0ohq3H5wDeMPRXmM1Lh3AmMZ1sNDXuUs_1Nyey0pqYe41V-aDZzVRObYkvyakU6lUFLegybs4sQxKE8nx8OkzqqOCGqZKQGzysmkWY0XNpfMOMFGPGs-pZqU8P1kFxM1dWJSA-fpNhbxaacdZadbsb98imQVRKI-WGPlEO8y2uaFBbSvF_1L0yQciRzJpXhABB7YN8elIAnSpJRx2Xx3HKzFYqRV4bFXe-8h1ppqUblRlrjCLuH2WNPMOroFb6XEkiE0CbHFIvGnucO4nq4T3vkYUrNWC034ziV5IDfacaGqU6b4ejqSBe_1-2gz-GOdaUrtygNzYtWYlVpWl0XUXcvJuuP3hUHqs1Aqs99I5WUBclqi82YHQRytlIQo_1G16YbDiw1Z9rkJliiPX3iofsMWcWbLsXrK2FBFK4vRj1Wh_1bRlYdCO9P0Hvn6MBCtkcuQCfPKe5HcSvowpofKFlA-Dvm3E7NYVIgPpTmYeH43DyAa9-plERGVchoC_1U1IEPaBIa5qQyqKBez0232oT1A3Ijuh0FjrYYm336YwspyITrdFgFtfDrqe7jUHBC3VbdXZR1pwYm-5A3APIfbNskqiNXJ2x2F57GjFncUiTwqECyZomj4bxFPT0IbGFmzVv97LbTTdIEItAQ2ABHRNGsFG9hKy38iuSUq3DMk5_1ONm7MC4dmEypy9lun1KQ5rf18Z0RyCjs3P76StUl5MnzHBZKUJ_1gWg18c5djMvxWWURwcObDrmgaC7Mssb0CN0CRxLtW0LAzJzxpC9V4-AX3Nc_11xMt45Ui6Mmb2N0k4oIOqGYKSMcTjUK-RoiIgPw00OgNc6qBr-i6h74ArH7iD5LLSq_1f8C0aDN7Y0M0pkEnkbFtF81u7VRQs9agF8LWNv6MnlvHQEzMXNjMtcC1eTTcGiTpiUnuVtXb1hsSFnCjoO9G4D5gHfn7smJaz50Qxq3CrdQLKfpWZ8Y-BukuvbVdVR2_1vg1_1kAlU3-5owOds16WQlTsHHmIb09B8E7PwIqgJfv1y8RtiLR3zhGPxRd4TEc32pHBzdRw"
}, function (res) {
console.log(res.statusCode);
var direct = url.parse(res.headers.location);
http.get(direct, function (res) {
console.log(res.statusCode);
});
});