如何使用 axios 进行 https 调用? [英] How to use axios to make an https call?

查看:607
本文介绍了如何使用 axios 进行 https 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有代理服务器的 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:error: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'

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-修复,但我建议使用第一种方法来确保最新的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆