无法使用https模块发出代理请求

时间:2017-12-01 08:17:33

标签: node.js https proxy

我在代理人后面。我正在尝试使用节点脚本从zip下载storage.googleapis.com文件。

我将HTTPS_PROXYHTTP_PROXY设置为我所在流程的环境变量,curl使用此命令并正确下载文件。

curl -O https://storage.googleapis.com/chromium-browser-snapshots/Mac/515411/chrome-mac.zip

这是节点脚本。当我在节点中运行它时,状态代码返回407。为什么?设置了代理程序代理程序,它与curl完全正常。

const URL = require('url')
const ProxyAgent = require('https-proxy-agent');
const getProxyForUrl = require('proxy-from-env').getProxyForUrl;
const https = require('https')

const url = 'https://storage.googleapis.com/chromium-browser-snapshots/Mac/515411/chrome-mac.zip'

function requestOptions(url, method = 'GET') {
    /** @type {Object} */
    const result = URL.parse(url);
    result.method = method;

    const proxyURL = getProxyForUrl(url);
    if (proxyURL) {
      /** @type {Object} */
      const parsedProxyURL = URL.parse(proxyURL);
      parsedProxyURL.secureProxy = parsedProxyURL.protocol === 'https:';

      result.agent = new ProxyAgent(parsedProxyURL);
    }
    console.log(result)
    return result;
  }

https.get(requestOptions(url), response => {
    console.log(response.statusCode)
})

1 个答案:

答案 0 :(得分:0)

添加result.rejectUnauthorized = false修复它

function requestOptions(url, method = 'GET') {
    /** @type {Object} */
    const result = URL.parse(url);
    result.method = method;

    const proxyURL = getProxyForUrl(url);
    if (proxyURL) {
      /** @type {Object} */
      const parsedProxyURL = URL.parse(proxyURL);
      parsedProxyURL.secureProxy = parsedProxyURL.protocol === 'https:';

      result.agent = new ProxyAgent(parsedProxyURL);
    }
    result.rejectUnauthorized = false
    return result;
}