http.get/request小提示

var options = {
host: 'www.google.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);
});
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
或者
var options = {
host: 'www.google.com',
port: 80,
path: '/index.html'
};

http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
配置参数时,要注意host: 'www.google.com'不能带http,可以是www.google.com也可以是
xxx.google.com

posted @ 2012-10-22 14:07  DolphinBoy  阅读(191)  评论(0编辑  收藏  举报