【问题标题】:Node.js HTTP GET not returning all dataNode.js HTTP GET 不返回所有数据
【发布时间】:2011-10-26 22:04:35
【问题描述】:

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} 时也不显示数据,但在使用@987654324 时它会将所有内容写入文件@。这是怎么回事?

预期结果:

# 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

四个缺失的记录在结果的中间。

【问题讨论】:

    标签: http curl node.js


    【解决方案1】:

    我比较确定是因为那个网站发送了\r,但没有发送\n。

    缓冲区内容:

    2c 30 30 0d 32 30
    

    0d 表示“回到行首”,0a 表示“下一行”。所以它总是回去,但从不写新行。它只是覆盖现有的输出。

    您可以尝试将 0d 替换为 0a 以查看所有内容。

    编辑:试过了,它有效。代码:

    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();
    

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 1970-01-01
      • 2015-11-22
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2021-04-11
      • 1970-01-01
      相关资源
      最近更新 更多