axios:https请求代理

时间:2017-04-05 19:55:58

标签: node.js ssl curl proxy axios

我正在尝试使用axios与代理服务器进行https呼叫:

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。

我收到此错误:

  

{错误:写EPROTO 140736580649920:错误:140770FC:SSL   套路:SSL23_GET_SERVER_HELLO:未知   协议:../ DEPS / OpenSSL的/ OpenSSL的/ SSL / s23_clnt.c:794:

为了确保问题出在axios而不是代理,我尝试了这个:

  

卷曲-x 52.8.172.72:4444 -L'https://www.walmart.com/ip/50676589'

这完全正常。

如何配置axios以使用代理和https URL?

8 个答案:

答案 0 :(得分:7)

如果使用https代理,则Axios https代理支持会失效。尝试使用http通过[httpsProxyAgent][1]传递代理。

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更改。

答案 1 :(得分:1)

尝试一下。那对我有用。

第一

npm安装axios-https-proxy-fix

然后

import axios from 'axios-https-proxy-fix'; 

const proxy = {
  host: 'some_ip',
  port: some_port_number,
  auth: {
    username: 'some_login',
    password: 'some_pass'
  }
};

async someMethod() {
  const result = await axios.get('some_https_link', {proxy});
}

答案 2 :(得分:1)

我知道这是一篇旧帖子,但我希望这个解决方案可以为任何面临 #reference dynamodb dynamodb = boto3.resource('dynamodb') #point at music table table = dynamodb.Table('Music') #get image url values from dynamodb imageUrl = table.scan(AttributesToGet=['img_url']) #print(urls) #loop through all items returned by dynamodb for url in imageUrl['Items']: image = url['img_url'] file_name = image.split('/')[-1] response = requests.get(image) file = open("/Users/abdotech/Desktop/images/"+file_name, "wb") for chunk in response: file.write(chunk) file.close() 的 SSL 问题的人节省时间。

  • 您可以使用 HTTP 代理,我建议使用 hpagent
Axios

答案 3 :(得分:1)

您可以通过查找 this issue

解决此问题

在此解决方案中,使用 http(s)Agent 代替代理接口。 为此,解决方案使用本机节点模块 https-proxy-agent

var ProxyAgent = require('https-proxy-agent');
var axios = require('axios');

const agent = ProxyAgent('http://username:pass@myproxy:port')

var config = {
  url: 'https://google.com',
  proxy: false,
  httpsAgent: agent
};

为了使其有效,代理属性必须等于 false。

答案 4 :(得分:0)

此错误是因为axios尝试通过https代理您的请求(它从您的网址获取),此票据会跟踪它:https://github.com/axios/axios/issues/925

答案 5 :(得分:0)

上周(2020年2月)更新依赖关系时,我试图解决服务停滞的原因而失去了一天的工作。 axios-https-proxy-fix将导致Axios无限期挂起,而不会超时或抛出与npm中其他库冲突的错误。使用节点隧道(https://github.com/koichik/node-tunnel)创建代理也可以。

const tunnel = require('tunnel');
class ApiService {
  get proxyRequest() {
    const agent = tunnel.httpsOverHttp({
      proxy: {
        host: 'http://proxy.example.com',
        port: 22225,
        proxyAuth: `username:password`,
      },
    });
    return axios.create({
      agent,
    })
  }
}

答案 6 :(得分:0)

https-proxy-agentnode-tunnel解决方案确实对我有用,但是它们都不支持使用NO_PROXY进行条件代理。

我发现global-agent是最好的解决方案,因为它修改了核心的http和https对象,并将自动应用于使用它们的任何库,包括axios,{{1 }},got

用法很简单。

request

npm i global-agent npm i -D @types/global-agent 添加到服务器的入口点(import 'global-agent/bootstrap';)。

使用这些环境变量运行,并确保HTTP_PROXY,HTTPS_PROXY不在环境中。

index.ts

这就是我最终使用它的方式。

export GLOBAL_AGENT_NO_PROXY='*.foo.com,baz.com'
export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8080

答案 7 :(得分:-1)

尝试在URL中明确指定端口:

const url = "https://walmart.com:443/ip/50676589"

如果您还需要HTTPS-over-HTTP隧道,则可以在this article中找到解决方案。

希望这有帮助,