【发布时间】:2018-02-24 16:13:21
【问题描述】:
我已经在 stackoverflow 和其他网站上搜索了几个教程和问题,但我仍然无法弄清楚为什么我的脚本会产生错误:socket hang up...
希望你们能帮助我 我在https://www.pixelstech.net/article/1445603357-A-HTTPS-client-and-HTTPS-server-demo-in-Java的教程中实现了一个https服务器
完美运行,客户端也运行良好。 但是当我想在 javascript 中创建一个请求并使用 node js 运行它时,我得到了已知的错误......
我的 .js 文件:
var https = require('https');
var data = JSON.stringify({
firstName: 'JoaquÌn',
});
function getCall() {
//initialize options values, the value of the method can be changed to POST to make https post calls
var options = {
host : 'localhost',
port : 9999,
path : '/',
rejectUnauthorized: false,
method : 'POST',
headers: {'Connection': 'keep-alive',
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': Buffer.byteLength(data)}
}
//making the https get call
var getReq = https.request(options, function(res) {
console.log("\nstatus code: ", res.statusCode);
res.on('data', function(data) {
console.log( JSON.parse(data) );
});
});
//end the request
getReq.end();
getReq.on('error', function(err){
console.log("Error: ", err);
});
}
getCall();
我的错误:
Error: { Error: socket hang up
at createHangUpError (_http_client.js:253:15)
at TLSSocket.socketOnEnd (_http_client.js:345:23)
at emitNone (events.js:91:20)
at TLSSocket.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9) code: 'ECONNRESET' }
IntelliJ 在我运行脚本时生成这部分:
SSLSession :
Protocol : TLSv1.2
Cipher suite : TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
Inut : POST / HTTP/1.1
Inut : Connection: keep-alive
Inut : Content-Type: application/json; charset=utf-8
Inut : Content-Length: 24
Inut : Host: localhost:9999
Inut :
我希望你能帮助我,因为我不知道为什么会出现错误,我尝试了几种解决方案,但没有一个对我有用......
谢谢马丁,祝你好运
【问题讨论】:
标签: javascript node.js https request