node.js 发送http 请求

  自己研究了一下 node.js 的 http模块  下面为想服务器发送请求的代码 ,通过学习了解http 请求的过程 ,node.js 对http请求的原始封装比较低,以前php 可以用$_GET ,$_POST直接拿到,node.js 需要自己把头信息,编码方式设置好,不过这样更利于了解http请求的整个过程

    

/**
* Created with JetBrains WebStorm.
* User: Administrator
* Date: 13-6-23
* Time: 下午10:23
* To change this template use File | Settings | File Templates.
*/


var http = require('http');
var options = {
host: 'p.3.cn',
//method:'GET'
path:'/prices/mgets?skuIds=J_905463,J_886150,J_867054,J_707049,J_820948,J_820962,J_882174,J_800196,J_871367,J_678632,J_863634,J_678636,J_728797,J_905463,J_886150,J_850810,J_812683,J_842273&type=1'

};

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);
});
});
//console.log(req);
// write data to request body
req.end();

posted on 2013-06-23 23:48  李乐已存在  阅读(2225)  评论(0编辑  收藏  举报

AmazingCounters.com