困惑:http.get请求结果正常,superAgent.get请求结果不对
环境: 0s:win7,32位。 node:v4.4.4
情景1:http.get请求,结果正常
var http = require('http');
http.get('http://hq.sinajs.cn/list=sh601006', function(res) {
var data = '';
res.on('data', function(chunk){
data += iconv.decode(chunk, 'GBK');
});
res.on('end', function(){
console.log(data);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
执行结果(这是想要的结果):
var hq_str_sh601006="大秦铁路,6.090,6.090,6.040,6.090,6.040,6.040,6.050,33330420,201913964.000,559480,6.040,938600,6.030,891600,6.020,1572400,6.
010,1421700,6.000,60400,6.050,635400,6.060,955500,6.070,1043876,6.080,992088,6.090,2016-07-08,15:00:00,00";
情景2: superAgent.get请求,没有得到预期的结果
当换成用superAgent来请求的时候问题出现了。
var superAgent = require('superagent');
var url = 'hq.sinajs.cn/list=sh600039';
superAgent
.get(url)
.end(function (err, res) {
if (err) {
return console.log(err);
}
logger.ndump('res', res);//换成console.log(res.text); 打印结果是undefined
});
执行结果(没有res.text):
VARIABLE<res>: {"req":{"method":"GET","url":"hq.sinajs.cn/list=sh600039"},"header":{"cache-co
ntrol":"no-cache","content-length":"170","connection":"Keep-Alive","content-type":"application/x-javascript; charset=GBK","content-encoding":"gz
ip"},"status":200}
情景三:浏览器直接访问,可以得到数据。
即使加入以下头部,请求也没有想要的数据。
superAgent
.get(url)
.set('Accept:text/html', 'application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
.set('Accept-Encoding', 'gzip, deflate, sdch')
.set('Accept-Language', 'zh-CN,zh;q=0.8,en;q=0.6')