我已经在stackoverflow和其他网站上搜索了几个教程和问题,但我仍然无法弄清楚为什么我的脚本会产生错误:套接字挂起......
希望你们能帮助我 我在https://www.pixelstech.net/article/1445603357-A-HTTPS-client-and-HTTPS-server-demo-in-Java
的教程中实现了一个https服务器完美运作,客户也可以。 但是,当我想在javascript中创建一个请求并使用节点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 :
我希望你能帮助我,因为我不知道为什么我在尝试了几个解决方案时遇到了错误,但没有一个能为我工作......
感谢马丁
答案 0 :(得分:0)
假设您已在配置的端口上运行HTTP服务器,则需要将host: 'localhost'
更改为host: 'http://localhost'
根据您设置的协议,它可以是http
或https
。