节点中的套接字挂断错误.js使用 https 时

Socket Hangup Error in Node.js on using https

本文关键字:js 使用 https 错误 套接字 节点      更新时间:2023-09-26

我正在尝试通过Nodejs使用HTTP GET方法,使用安全连接https。这是我的代码:

var https = require('https');
https.globalAgent.options.secureProtocol = 'SSLv3_method';
var options = {
  host: 'my_proxy_address',
  port: 3128,
  path: 'https://birra-io2014.appspot.com/_ah/api/birra/v1/beer',
  method: 'GET',
  headers: {
    accept: '*/*'
  }
};
var req = https.request(options, function(res) {
  console.log(res.statusCode);
  res.on('data', function(d) {
    process.stdout.write(d);
  });
});
req.end();
req.on('error', function(e) {
  console.error(e);
});

当我运行这个时,我收到一个错误:

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

我不能使用HTTP,因为appspot需要https。请帮忙!提前谢谢。

尝试以下代码。(它为我工作)

var https = require('https');
var options = {
  port: 443,
  host: 'birra-io2014.appspot.com',
  path: '/_ah/api/birra/v1/beer',
  method: 'GET',
  headers: {
    accept: '*/*'
  }
};
var req = https.request(options, function(res) {
  console.log("statusCode: ", res.statusCode);
  console.log("headers: ", res.headers);
  res.on('data', function(d) {
    process.stdout.write(d);
  });
});
req.end();
req.on('error', function(e) {
  console.error('ERROR object ==>' + e);
});