Node.js Https请求错误 [英] Node.js Https request Error

查看:159
本文介绍了Node.js Https请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过文档中的示例,但效果很好。

I've tried the sample from the documentation and it works great.

但当我将URL更改为 https://api.mercadolibre.com/sites/ 时,请求会挂起。我唯一得到的是:

But when I change the URL to https://api.mercadolibre.com/sites/, the request hangs. The only thing I get is:

{ [Error: socket hang up] code: 'ECONNRESET' }

这是我的代码:

var https = require('https');

this.dispatch = function(req, res) {
  var renderHtml = function(content) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(content, 'utf-8');
  }

  var parts = req.url.split('/');

  var options = {
    host: 'api.mercadolibre.com',
    port: 443,
    path: '/sites/',
    method: 'GET'
  };

  var request = https.request(options, function(res) {
    console.log("statusCode: ", res.statusCode);
    console.log("headers: ", res.headers);

    res.on('data', function(d) {
      process.stdout.write(d);
    });
  });

  request.on('error', function(e) {
    console.error('error');
    console.error(e);
  });

  request.end();
  return 'item id:' + parts[2];
};

我尝试过使用curl,soapui和浏览器。在所有情况下效果都很好,但是对于node.js它没有。

I've tried with curl, soapui and with a browser. On all cases works great, but with node.js it doesn't.

如何获得有关正在发生的事情的更多数据?

How can I get more data on what's going on?

已添加

使用curl我可以:curl --sslv3 https://api.mercadolibre.com/sites/ 有效。
我在centos 6中测试过同样的功能。
我重新安装了节点,这次是从源头开始,同样的问题。我的Os是ubuntu 12.04。
谢谢。

With curl i do: curl --sslv3 https://api.mercadolibre.com/sites/ works. I've test same in centos 6 and works too. I've reinstalled node, this time from source, same problem. My Os is ubuntu 12.04. Thanks.

推荐答案

我不确定 api.mercadolibre.com 网站,但如果我删除 port param,我可以调用API,如下面的代码:

I'm not sure about api.mercadolibre.com site, but I can call API if I remove port param, like following code:

var options = {
    host: 'api.mercadolibre.com',
    path: '/sites/',
    method: 'GET'
};

我们还需要添加param来支持SSL版本3:

And we also need add param to support SSL version 3:

https.globalAgent.options.secureProtocol = 'SSLv3_method';

这篇关于Node.js Https请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆