Node.js 三種http請求方法

'Accept-Encoding':'gzip, deflate',

'Accept-Language':'zh-CN,zh;q=0.8',

'Connection':'keep-alive',

'Content-Length':postData.length,

'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',

'Cookie':'',

'Host':'127.0.0.1',

'Origin':'127.0.0.1',

'Referer':'http://www.imooc.com',

'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36',

'X-Requested-With':'XMLHttpRequest'

}

}

var req = http.request(options,function(res){

var buffers = [];

res.on('data',function(chunk){

buffers.push(chunk);

})

res.on('end',function(){

return callback(null,buffers);

})

})

req.on('error',function(e){

return callback(e,null);

})

req.write(postData)

req.end();

只需要正確設置好header和請求地址,請求參數就ok了,返回的數據是buffer形式,所以都push進了buffer裡,數據接收完畢之後也就是end之後將數據返回就可以了。


分享到:


相關文章: