【问题标题】:Socket hang up on node js https request套接字挂断节点js https请求
【发布时间】: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


    【解决方案1】:

    假设您已经在配置的端口上运行了 HTTP 服务器,您需要将 host: 'localhost' 更改为 host: 'http://localhost'

    根据您设置的协议,它可以是httphttps

    【讨论】:

    • 嘿 :) 感谢您的响应,但是在将行从 localhost 更改为 http localhost 后,我​​收到了此响应:Error: { Error: getaddrinfo ENOTFOUND https://localhost https://localhost:9999 at errnoException (dns.js:28:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26) code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'https://localhost', host: 'https://localhost', port: 9999 }
    • 你能把更新的代码放上去吗?这些错误表明您使用的是https。我假设服务器已经在上述端口上运行
    • 嘿,很抱歉回复晚了,但我们改变了技术方式,我们正在使用 Wildfly 服务器 (Wildfly Swarm) 与我们的前端进行通信 :) 但是非常感谢 :)跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2013-11-16
    • 2015-05-03
    • 1970-01-01
    • 2018-01-29
    相关资源
    最近更新 更多