http - Node.js HTTP GET 不返回所有数据

标签 http curl node.js

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/

相关文章:

javascript - http ://example. com 和 www.example.com 之间真的有区别吗?

iOS 对网络服务的基本请求

c# - CURL 到 WCF 的更好方法

node.js - Mongoose 查询,选择 10 个最近的文档而不改变顺序

javascript - 带书架的 Node.js;为与 ID 数组的多对多关系设置值

php - 使用 PHP 和 RESTful API 获取和发送文件

不允许返回消息正文的 HTTP 状态代码

linux - 创建一个 shell,使用 curl 在 dropfile.to 上下载文件

php - 访问来自 PHP/JQuery.ajax 的传入 PUT 数据以及如何处理它?

javascript - 如何使用 Node 请求模块正确编码表情符号?