I use http.request to curl a webpage,this is my code;
var curlUtil=function(option,callback){
var bufferHelper = new BufferHelper();
console.log(option);
var data='';
var req=http.request(option,function(res){
if(res.statusCode==301||res.statusCode==302){
var link=res.headers.location;
console.log('redirect:'+link);
curlUtil(link,function(data){
callback(data);
});
return;
}
res.on('data',function(chunk){
bufferHelper.concat(chunk);
});
res.on('end',function(){
var data=bufferHelper.toBuffer().toString();
callback(data);
});
});
req.end();
};
sometimes it works fine, but sometimes it will get broken data, and the req.on('end');
will be called when there is still data waiting to get. I don't know how this happen, but it did happen, anyone who can tell me what should i do?