0

我的应用在 localhost 上运行良好。我们的 Cloud Foundry 设置不知何故没有出站连接......所以我被告知我可以通过代理获得出站连接。

我需要在我的法拉第请求中更改哪些内容才能使用该代理?

我的代码以及我应该如何连接。

url = "#{@config[:host]}#{@config[:login_path]}"
c = Faraday.new(:url => url,
                :request => { :timeout => 20, :open_timeout => 5},
                :ssl => {:verify => false}) do |f|
  f.request :url_encoded
  f.adapter Faraday.default_adapter
  f.response :json, :content_type => /\bjson$/
end

  @resp = c.post(url, {username: username, password: password,
    deviceId: @config[:device_id], deviceName: @config[:device_name], deviceType: @config[:device_type],
    appId: @config[:app_id], scope: @config[:scope]})

这就是我应该如何使用已设置的代理。

“一旦您的服务被绑定,应用程序将在VCAP_SERVICES环境变量中具有 JSON 格式的服务定义,您可以使用cf env x-explorer命令查看格式。您的应用程序将需要使用给定的凭据解析和设置代理。

这是凭据的示例。您可以仅使用或uri单独使用参数:hostport和"usernamepassword

uri: http://2abcf906-127e-495c-ae71-c69712862292:616e714aea0cfc81de67ad1445cf1448e38d6a8db345a452c0e1ddc830b8f132@f.services.g1.app.cloud.my_company.net:49349
host: f.services.g1.app.cloud.my_company.net
port: 49349
username: 2abcf906-127e-495c-ae71-c69712862292
password: 616e714aea0cfc81de67ad1445cf1448e38d6a8db345a452c0e1ddc830b8f132

同样,应用程序在 localhost 上运行良好。与公司 Cloud Foundry 设置没有出站连接。我已经绑定了服务,所以没问题。

我要添加什么来让我的出站请求通过代理?

4

1 回答 1

2

尝试这个

url = "#{@config[:host]}#{@config[:login_path]}"
c = Faraday.new(:url => url,
                :request => { :timeout => 20, :open_timeout => 5},
                :ssl => {:verify => false}) do |f|
  f.request :url_encoded
  f.adapter Faraday.default_adapter
  f.response :json, :content_type => /\bjson$/url = "#{@config[:host]}#{@config[:login_path]}"
c = Faraday.new(:url => url,
                :request => { :timeout => 20, :open_timeout => 5},
                :ssl => {:verify => false}) do |f|
  f.request :url_encoded
  f.adapter Faraday.default_adapter
  f.response :json, :content_type => /\bjson$/
  f.proxy = "http://2abcf906-127e-495c-ae71-c69712862292:616e714aea0cfc81de67ad1445cf1448e38d6a8db345a452c0e1ddc830b8f132@f.services.g1.app.cloud.my_company.net:49349"
end

  @resp = c.post(url, {username: username, password: password,
    deviceId: @config[:device_id], deviceName: @config[:device_name], deviceType: @config[:device_type],
    appId: @config[:app_id], scope: @config[:scope]})

end

  @resp = c.post(url, {username: username, password: password,
    deviceId: @config[:device_id], deviceName: @config[:device_name], deviceType: @config[:device_type],
    appId: @config[:app_id], scope: @config[:scope]})

在这里,他们说如何根据法拉第请求设置代理

conn = Faraday.new(url, ssl: {verify:false}) do |conn|
  # middleware ...
  conn.adapter <adapter>

  conn.proxy "http://localhost:80"
end

https://github.com/lostisland/faraday/issues/221

于 2015-07-24T22:14:32.120 回答