###想请教一下如何用nodejs模仿客户端访问? 以前记得有http.createClient这个函数可以模仿客户端,现在变成了http.request(options,callback)的模式,可是试了一下没有成功 请问有什么好的方法解决这样的问题吗?
var http = require(‘http’);
var options = { hostname: ‘www.baidu.com’, port: 80, path: ‘/upload’, method: ‘POST’ };
var req = http.request(options, function(res) { console.log('STATUS: ’ + res.statusCode); console.log('HEADERS: ’ + JSON.stringify(res.headers)); res.setEncoding(‘utf8’); res.on(‘data’, function (chunk) { console.log('BODY: ’ + chunk); }); });
req.on(‘error’, function(e) { console.log('problem with request: ’ + e.message); });
// write data to request body req.write(‘data\n’); req.write(‘data\n’); req.end();