模拟登陆时,怎么解决重定向
用的http模块代码如下: 怎么解决重定向问题,请大神们指点一下。
var http=require("http");
var querystring=require("querystring");
var fs=require("fs");
var url="http://210.44.176.227:88";
var contents=querystring.stringify({
'__EVENTTARGET':'',
'__EVENTARGUMENT':'',
'__VIEWSTATE':'/wEPDwdQN0y5hw=',
'Button1': '登录',
'RadMultiPage1_ClientState':''
});
var options={
host:"210.44.176.227",
port:"88",
path:"/Default.aspx",
method:"post",
headers:{
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4",
"Cache-Control":"max-age=0",
"Connection":"keep-alive",
"Content-Length":contents.length,
"Content-Type":"application/x-www-form-urlencoded",
"Host":"210.44.176.227:88",
"Origin":"http://210.44.176.227:88",
"Referer":"http://210.44.176.227:88/Default.aspx",
"Upgrade-Insecure-Requests":"1",
"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2587.3 Safari/537.36"
}
};
var req=http.request(options,function(res){
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();
req.on("error",function(err){
console.log(err);
});
req.on("response",function(response){
//这里的话,response就会包含返回的cookie,但是我不知道如果这里有重定向该怎么做
var option2={
method:"GET",
host:"210.44.176.227",
port:"88",
path:"/Desk",//目标页面
headers:{
"cookie":response.headers["set-cookie"].toString()//这个就是cookie,可以用util解析一下response.headers,看看里面到底有什么
}
}
var request2=http.request(option2);
request2.end();
request2.on("error",function(err){console.log(err);});
request2.on("response",function(response2){
})
})
文件输出的
<head><title>文档已移动</title></head>
<body><h1>对象已移动</h1>可在<a HREF="http://210.44.176.227:88/Desk/">此处</a>找到该文档</body>