首页 > 解决方案 > https节点挂起403,如何捕捉错误?

问题描述

使用 https 在节点中调用 url 时处理 403 的最佳方法是什么?

我有一个简单的类,它调用一个 URL,当从 vpn 调用它时,它预计会返回 403 .. 但脚本没有得到 403,而是挂起,它从不拒绝承诺或解决它:/

const https = require('https');

class SecondaryURLScraper {
  getHeaders (url) {
    const urlParts = new URL(url);
    return {
      /* Mimicking the browsers request headers */
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
      // 'Accept-Encoding': 'gzip, deflate, br', // todo, add the gzip decompression to use these headers
      'Accept-Language': 'en-GB,en;q=0.5',
      'Cache-Control': 'max-age=0',
      'Connection': 'keep-alive',
      'DNT': 1,
      'Host': urlParts.hostname,
      'TE': 'Trailers',
      'Upgrade-Insecure-Requests': 1,
      'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0',
    };
  }

  https (url) {
    return new Promise((resolve, reject) => {
      https.get(url, { headers: this.getHeaders(url) }, (res) => {
        console.log(res);
        if (res.statusCode === 200) {
          let data = '';
          res.on('data', (chunk) => {
            data += chunk;
          });
          res.on('end', () => {
            resolve(data);
          });
        } else {
          console.log('non 200');
          reject(new Error('Non 200'));
        }
      }).on('error', (err) => {
        console.log('error', err);
        reject(err);
      });
    });
  }
}

const secondaryURLScraper = new SecondaryURLScraper();
secondaryURLScraper
  .https('https://www.abercrombie.com/shop/eu/p/classic-hoodie-42543819?categoryId=84884&seq=15&faceout=prod1')
  .then((d) => {
    console.log(d);
  })
  .catch((e) => {
    console.error(e);
  });

最后添加.end()也无济于事..它只是继续挂起:/

标签: node.jshttps

解决方案


推荐阅读