【发布时间】:2014-04-28 00:09:40
【问题描述】:
我有这个小代码 sn-p,它针对托管在 localhost 上的端点
var https = require('https');
var options = {
hostname: 'localhost',
port: 443,
path: '/',
method: 'GET',
agent: false
};
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(e);
});
我总是得到错误:
{ [Error: socket hang up] 代码: 'ECONNRESET', sslError: undefined }
似乎端点甚至没有收到请求,因为它没有被它捕获并且没有发生超时。
如果我从浏览器尝试像 https://localhost 这样的请求,它会成功发送。
如果我只是将代码中的主机更改为 encrypted.google.com 之类的内容,它也可以成功运行。
有人知道为什么会发生这种情况吗?
编辑:我也尝试添加浏览器发送的相同标头,例如接受、用户代理等,但仍然无法正常工作
Edit2:这是我记录时出现的堆栈跟踪:
Error: socket hang up
at SecurePair.error (tls.js:1013:23)
at EncryptedStream.CryptoStream._done (tls.js:705:22)
at CleartextStream.read [as _read] (tls.js:496:24)
at CleartextStream.Readable.read (_stream_readable.js:320:10)
at EncryptedStream.onCryptoStreamFinish (tls.js:301:47)
at EncryptedStream.g (events.js:180:16)
at EncryptedStream.EventEmitter.emit (events.js:117:20)
at finishMaybe (_stream_writable.js:360:12)
at endWritable (_stream_writable.js:367:3)
at EncryptedStream.Writable.end (_stream_writable.js:345:5)
【问题讨论】:
-
你能在选项中试试 strictSSL: false 吗?
标签: javascript node.js sockets https localhost