gpt4 book ai didi

Node.js - 请求 : Specifying custom agent that can handle http & https

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:56 25 4
gpt4 key购买 nike

我使用 request.defaults(..) - 除此之外 - 为我的请求设置自定义 http 代理(设置 maxSockets保持事件)。

const agent = new http.Agent({
keepAlive: true,
maxSockets: 42
})
const requestWithCustomAgent = request.defaults({
agent,
json: true,
headers: {
'User-Agent': 'amazing agent'
}
})

使用方案 https 访问 URL 时,会出现错误:错误:协议(protocol)“https:”不支持。应为“http:”https.Agent 和方案 http 反之亦然。

有没有办法“告诉”请求使用一个代理处理 http 请求,并使用另一个代理处理 https 请求?

人们可以投入一些 unicorn 的血液并执行黑魔法,例如:

if(url.startsWith('https') {/* use on agent */} else { /* use the other */}

但这感觉有点hack'ish。

是这样的:

const requestWithCustomAgent = request.defaults({
agents: {
'http': httpAgent,
'https': httpsAgent
},
json: true,
headers: {
'User-Agent': 'amazing agent'
}
})

可能吗?

感谢帮助;)

最佳答案

如果您使用 agentOptions 而不是传递 http(s).Agent 实例,request 将为您为每个方案创建一个代理使用的是:

const requestWithCustomAgent = request.defaults({
agentOptions : {
keepAlive : true,
maxSockets : 42
},
json : true,
headers : {
'User-Agent': 'amazing agent'
}
})

关于Node.js - 请求 : Specifying custom agent that can handle http & https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45547113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com