我在代理人后面。我正在尝试使用节点脚本从zip
下载storage.googleapis.com
文件。
我将HTTPS_PROXY
和HTTP_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)
})
答案 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;
}