Node 的 HTTP 模块没有从这个 URL 返回所有预期的数据:http://itradedata.co.za/pmchart/prices/GetPricesTRI.asp?Shortname=ACE&numprices=5
浏览器查看价格记录有5条,Node的HTTP GET数据中只有一条。数据包嗅探显示所有数据进入,那么为什么它不在输出中?
var http = require('http'),
host = 'itradedata.co.za',
records = 5,
url = '/pmchart/prices/GetPricesTRI.asp?Shortname=ACE&numprices=' + records,
client = http.createClient(80, host),
headers = { 'host': host },
req;
req = client.request('GET', url, headers);
req.on('response', function(res) {
console.log(res.statusCode);
res.on('data', function (chunk) {
console.log(chunk.toString());
});
});
req.end();
问题似乎出在服务器返回数据的方式上...... cURL 在终端中运行 curl {url}
时也不显示数据,但它使用 curl {url} -o {file}
时将所有内容写入文件。这是怎么回事?
预期结果:
# Attempting to get 5 prices for theTicker: ACE<BR>
# Trying JSECODE<BR>
# Sending prices for Entity_ID: 17352 Shortname: ShareID: JSECode: <BR>
2011-8-15,46,46,46,46,0,08268
2011-8-12,46,46,46,46,51,0068
2011-8-11,46,46,46,46,51,0068
2011-8-10,46,46,46,46,51,0068
2011-8-8,46,46,46,46,51,00068
实际结果:
# Attempting to get 5 prices for theTicker: ACE<BR>
# Trying JSECODE<BR>
# Sending prices for Entity_ID: 17352 Shortname: ShareID: JSECode: <BR>
2011-8-8,46,46,46,46,51,00068
四个缺失的记录在结果的中间。
最佳答案
我比较确定是因为那个站点发送了\r
,但是没有发送\n
。
缓冲区内容:
2c 30 30 0d 32 30
0d 表示“回到行首”,0a 表示“下一行”。所以它总是返回,但从不写新行。它只是覆盖现有的输出。
您可以尝试用 0a 替换 0d 以查看所有内容。
编辑:试过了,有效。代码:
req = client.request('GET', url, headers);
req.on('response', function(res) {
console.log(res.statusCode);
res.on('data', function (chunk) {
for (var i=0; i<chunk.length; i++)
if (chunk[i] === 0xd) chunk[i] = 0xa
console.log(chunk.toString());
});
});
req.end();
关于http - Node.js HTTP GET 不返回所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7059268/