在 Savon 中使用 HTTP 代理

Use HTTP proxy with Savon

我需要使用 Heroku 的 IP 白名单访问服务,因此需要 HTTP 代理。

我配置了 Proximo 插件,并且有一个代理 url 看起来大约像 http://proxy:secret-key@proxy-0-0-0-0.proximo.io

在 Savon,我试过像这样配置代理:

client = Savon.client(wsdl: my_wsdl, proxy: "http://proxy:secret-key@proxy-0-0-0-0.proximo.io")

但是当我提出请求时:

client.operations

我收到代理错误:

Wasabi::Resolver::HTTPError: Error: 407 for url http://mywsdl
        from /Users/ahamon/.gem/ruby/2.3.0/gems/wasabi3.5.0/lib/wasabi/resolver.rb:45:in `load_from_remote'
        from /Users/ahamon/.gem/ruby/2.3.0/gems/wasabi3.5.0/lib/wasabi/resolver.rb:33:in `resolve'
        from /Users/ahamon/.gem/ruby/2.3.0/gems/wasabi3.5.0/lib/wasabi/document.rb:142:in `xml'
        from /Users/ahamon/.gem/ruby/2.3.0/gems/wasabi-3.5.0/lib/wasabi/document.rb:160:in `parse'
        from /Users/ahamon/.gem/ruby/2.3.0/gems/wasabi-3.5.0/lib/wasabi/document.rb:147:in `parser'
        from /Users/ahamon/.gem/ruby/2.3.0/gems/wasabi-3.5.0/lib/wasabi/document.rb:64:in `soap_actions'
        from /Users/ahamon/.gem/ruby/2.3.0/gems/savon-2.11.1/lib/savon/client.rb:28:in `operations'
        from (irb):7
        from /Users/ahamon/.gem/ruby/2.3.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
        from /Users/ahamon/.gem/ruby/2.3.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
        from /Users/ahamon/.gem/ruby/2.3.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
        from /Users/ahamon/.gem/ruby/2.3.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from /Users/ahamon/.gem/ruby/2.3.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

不过,没有代理选项一切正常。

我认为这应该可行

client = Savon.client(wsdl: my_wsdl, proxy: "http://proxy-0-0-0-0.proximo.io", basic_auth: ["proxy-user", "proxy-secret"])

我想我找到了解决办法。 不便之处在于手动获取 WSDL 并将其保存到本地文件中。 例如

curl http://www.webservicesx.net/globalweather.asmx?wsdl > globalweather.wsdl

您必须将以下内容添加到您的客户定义中

...
secret = Base64.strict_encode64("#{user}:#{password}")
client = Savon.client(
    wsdl: 'globalweather.wsdl',
    proxy: "http://my-proxy.example.com:8080",
    headers: { "Proxy-Authorization" => "Basic #{secret}" },
    ...
)

基本上就是这样。 您也可以通过直接指定 namespaceendpoint 来完全不使用 WSDL。

Savon 使用 Wasabi 来处理 WSDL。它还从服务器获取文档。 Wasabi 不使用 Savon 的客户端定义中的参数。要更改它,必须扩展 Wasabi gem.

可在此处找到 运行 脚本:http://pastebin.com/t8NTuGKK