如何在代理后面使用 httpotion?

How can I use httpotion behind the proxy?

(编辑:我使用 HTTPoison 的 get! 函数解决了这个问题。

HTTPoison.start
HTTPoison.get!("httpbin.org/get", [], [{:proxy, {"proxy.mydomain.com", 8080}}])

我是使用 elixir 的新手。作为第一步,我在 httpotion 上尝试了示例应用程序。

iex> response = HTTPotion.get "httpbin.org/get"

但是,它无法到达代理后面的站点。

iex(1)> res = HTTPotion.get "httpbin.org/get"
** (HTTPotion.HTTPError) nxdomain
    (httpotion) lib/httpotion.ex:195: HTTPotion.handle_response/1

没有代理,这样就成功了;

iex(1)> res = HTTPotion.get "httpbin.org/get"
%HTTPotion.Response{body: "{\n  \"args\": {}, \n  \"headers\": {\n        \"Content-Length\": \"0\", \n    \"Host\": \"httpbin.org\"\n  }, \n  \"origin\": \"191.238.84.51\", \n  \"url\": \"http://httpbin.org/get\"\n}\n",
 headers: ["Access-Control-Allow-Credentials": "true",...

我尝试通过阅读 httpotion 依赖的 ibrowse 来设置代理参数,例如;

req = HTTPotion.get("httpbin.org/get", [{:proxy_host, "proxy.mydomain.com"}, {:proxy_port, 8080}])

但是结果是一样的

如何设置httpotion的代理参数?或者在 elixir 上是否有任何可以处理代理的用于 HTTP 访问的替代库?

我的环境是Ubuntu14.04.2,环境变量(http_proxy、https_proxy、HTTP_PROXY和HTTPS_PROXY)设置正确。

看看httpoison tests :D

以下是使用代理执行获取请求的方法:

HTTPoison.get!("http://www.google.com", [], [{:proxy, "proxy.company.address:port"}])

我刚刚通过阅读源代码自己弄明白了这一点,但我现在注意到它记录在最新的自述文件中...

简短版本(因为这个答案在 Google 搜索中比自述文件更早出现)是您需要将参数直接传递给 ibrowse,您使用 :ibrowse 选项执行此操作,然后还要注意 ibrowse通常采用字符列表

所以,例如:

HTTPotion.get "httpbin.org/get", [ ibrowse: [ proxy_host: 'some.host', proxy_port: 8080 ] ]

请注意,httpotion 似乎并没有很好地捕获异常,因为它不是“!”函数的版本...不使用字符列表或类似的将导致各种难以理解的异常...