【问题标题】:Error: socket hang up using https错误:使用 https 挂断套接字
【发布时间】:2018-01-29 22:50:36
【问题描述】:

我的应用程序在一段时间后崩溃并出现此错误:

{ 错误:连接 ETIMEDOUT 31.13.92.51:443 在 Object.exports._errnoException (util.js:1018:11) 在exports._exceptionWithHostPort (util.js:1041:20) 在 TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14) 代码:'ETIMEDOUT',errno:'ETIMEDOUT',系统调用:'connect',
地址:'31.13.92.51',端口:443 }

{ 错误:套接字挂起 在 createHangUpError (_http_client.js:253:15) 在 TLSSocket.socketCloseListener (_http_client.js:285:23) 在 emitOne (events.js:101:20) 在 TLSSocket.emit (events.js:188:7) 在 _handle.close (net.js:497:12) 在 TCP.done [as _onclose] (_tls_wrap.js:332:7) 代码:'ECONNRESET' }

我的代码没有发现任何错误,我该如何处理这个错误?

try {
    https.get('https://www.instagram.com/'+username+'/?__a=1', function(resp){
        var body = '';
        resp.setEncoding('utf8');
        resp.on('data', function (chunk) {
            body += chunk;
        });
        resp.on('end', function () {
            try {
                var json = JSON.parse(body);
            } catch (error) {
                callback('Unable to fetch user info from Instagram.', null);
                return;
            }
            callback(null, json.user);
        });
    });
}catch (error){
    callback("Connection to Instagram failed.", null);
}

【问题讨论】:

    标签: javascript node.js sockets


    【解决方案1】:

    我已经尝试过使用https 模块,我遇到了同样的问题,所以我决定使用另一个模块request 模块,它似乎工作正常。我不知道为什么,但 https 无法从 instagram.com 得到响应,因为我已经尝试过了。这是你可以尝试的,因为它对我有用。安装request模块

        var request = require("request");
        var ext = '/?__a=1';
        var username = //username here;
    
    
        request
          .get('https://www.instagram.com/'+ username + ext)
          .on('response', function(response) {
            console.log(response.statusCode) // 200 is what I get from request
            console.log(response.headers['content-type'])
          })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-28
      • 2015-05-03
      • 2014-07-27
      • 2021-01-02
      • 1970-01-01
      • 2015-10-30
      • 2014-10-06
      相关资源
      最近更新 更多