如何使用axios进行https呼叫? [英] How to use axios to make an https call?
问题描述
我正在尝试将 axios 与代理服务器一起使用以进行https呼叫:
I am trying to use axios with a proxy server to make an https call:
const url = "https://walmart.com/ip/50676589"
var config = { proxy: { host: proxy.ip, port: proxy.port } }
axios.get(url, config)
.then(result => {})
.catch(error => {console.log(error)})
我使用的代理服务器都在美国,高度匿名,并支持HTTP和HTTPS.
The proxy servers I am using are all in the United States, highly anonymous, with support for HTTP and HTTPS.
我收到此错误:
{错误:写EPROTO 140736580649920:错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知 协议:../deps/openssl/openssl/ssl/s23_clnt.c:794:
{ Error: write EPROTO 140736580649920:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:794:
为了确保问题出在axios而非代理上,我尝试了以下方法:
In order to ensure that the problem is with axios and NOT the proxy, I tried this:
curl -x 52.8.172.72:4444 -L' https://www.walmart.com/ip/50676589 '
这完全可以.
我如何配置axios以使用代理和https URL?
How do I configure axios to work with proxies and https URL's?
推荐答案
如果使用https代理,Axios https代理支持将被取消.尝试使用http通过[httpsProxyAgent][1]
传递代理.
Axios https proxy support is borked if using https proxies. Try passing the proxy through [httpsProxyAgent][1]
using http.
var axios = require('axios');
let httpsProxyAgent = require('https-proxy-agent');
var agent = new httpsProxyAgent('http://username:pass@myproxy:port');
var config = {
url: 'https://google.com',
httpsAgent: agent
}
axios.request(config).then((res) => console.log(res)).catch(err => console.log(err))
或者,有一个Axios的分支包含以下内容: axios-https-proxy -fix ,但我建议您采用第一种方法来确保最新的Axios更改.
Alternatively there is a fork of Axios that incorporates this: axios-https-proxy-fix but I'd recommend the first method to ensure latest Axios changes.
这篇关于如何使用axios进行https呼叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!