最近需要使用 Nodejs 读取本地文件中的数据构造请求去批量请求 CGI 获取数据,这样就不用手工搬砖了。因为需要携带 Cookie,故使用 POST 方法。
代码
// 读取本地文件
var fs = require("fs");
var readline = require('readline');
var rd = readline.createInterface({
input: fs.createReadStream('./test.md'),
output: process.stdout,
terminal: false
});
var http = require("http");
var url = require("url");
var cookie = "K1:v1"
rd.on('line', function(line) {
var parts = line.split(',')
keyword = parts[0]
newIndex = parts[1]
strUrl = "http://wwwtest.com" + keyword + "&start_time=1485241176.43&end_time=1492930776.43&version=0"
var parse = url.parse(strUrl);
// 待发送的数据
var postStr = "test";
var options = {
"method": "POST",
"host": parse.hostname,
"path": parse.path,
"port": parse.port,
"headers": {
"Content-Length": postStr.length,
'Cookie': cookie
}
};
var req = http.request(options, function(res) {
word = parts[0]
new_index = parts[1]
res.setEncoding("utf-8");
var resData = [];
res.on("data", function(chunk) {
resData.push(chunk);
}).on("end", function() {
console.log(resData.join(""));
});
});
req.write(postStr);
req.end();
});