justrun
V2EX  ›  问与答

Node https get 请求超时

  •  
  •   justrun · Nov 1, 2016 · 1084 views
    This topic created in 3483 days ago, the information mentioned may be changed or developed.

    测试 google cse 的 api ,在 chrome 和 postman 中都能在 1s 左右获取到返回的 json (网络畅通,未科学上网),可是在 Node 中使用 https 的代码请求就一直是超时状态,请问下有可能哪里出了问题?新人不太懂,谢谢!

    Node 中的代码, key 和 id 替换掉了:

    var options = {
      hostname: 'www.googleapis.com',
      port: null,
      path: '/customsearch/v1?key=我的 key&cx=我的 id&num=10&q=python',
      method: 'GET'
    };
    
    var req = https.request(options, (res) => {
      console.log('statusCode:', res.statusCode);
      console.log('headers:', res.headers);
    
      res.on('data', (d) => {
        process.stdout.write(d);
      });
    });
    req.end();
    
    req.on('error', (e) => {
      console.error(e);
    });
    

    报错 log :

    { [Error: connect ETIMEDOUT 172.217.26.106:443]
      code: 'ETIMEDOUT',
      errno: 'ETIMEDOUT',
      syscall: 'connect',
      address: '172.217.26.106',
      port: 443 }
    
    2 replies    2016-11-02 00:40:54 +08:00
    justrun
        1
    justrun  
    OP
       Nov 2, 2016
    写了几句 python 试了下也能正常得到响应数据。。
    xxxyyy
        2
    xxxyyy  
       Nov 2, 2016 via Android
    是否在 hosts 里绑定了 www.googleapis.com
    自己 ping 172.217.26.106 可以 ping 的通吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3030 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 12:02 · PVG 20:02 · LAX 05:02 · JFK 08:02
    ♥ Do have faith in what you're doing.