node.js - Node 获取请求响应不完整

标签 node.js ssl httprequest

当我使用给定 url 的 Node 请求库请求图像时,加载的图像不完整。存储加载的图像后,它看起来像 https://ibb.co/i5xVAR

但是请求完成时没有错误并且状态代码为 200。对我来说,ssl 连接似乎已关闭。浏览器或 curl 等其他工具将图像完整传输。

const request = require('request');
    const r1 = request({
        url: 'https://open.hpi.de/files/f1d16619-9813-4d59-96b3-d84908929b23',
        encoding: 'binary'
      }, (err, response, body) => {
        if (err) {
         console.log(err);
         return;
        }
        // complete file should be loaded
        // content and body length should match
        // read ECONNRESET should not be thrown
        console.log('body length', body.length);
        console.log('response content length', response.headers['content-length']);
      });

最佳答案

open.hpi.de 主机过早关闭连接。您可以向请求添加 Connection: keep-alive header ,连接将保持打开状态,直到传输实际完成:

const request = require('request');
const r1 = request({
  url: 'https://open.hpi.de/files/f1d16619-9813-4d59-96b3-d84908929b23',
  encoding: 'binary',
  headers: {
    "Connection": "keep-alive"
  }
}, (err, response, body) => {
  // do the things
});

关于node.js - Node 获取请求响应不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47812126/

相关文章:

python-3.x - python+selenium+phantomjs 无法处理 `adding certification is strongly advised` 的 https url

iOS AF网络 3.0 : Is there a faster way to send multiple API requests and wait until all of it is finished?

Android 无法使用 Volley 库访问 URL

javascript - 使用 Bluebird 在 Promises 中包装 Node.js 回调

php - 在 MVC 框架中,我们应该将 NODE.js 文件放在哪里?

node.js - JSPM GitHub 凭据失败

ssl - 在 CentOS 中创建由 CA 签名的 SSL key

c# - 如何在 Machine Root store 中安装 Fiddler 的根证书

java - Spring,如何通过 header 管理HTTP请求?

javascript - Node.js 子进程中的数据丢失