【问题标题】:Getting {error,connect_timeout} message while using Hackney使用 Hackney 时收到 {error,connect_timeout} 消息
【发布时间】:2016-06-07 20:00:00
【问题描述】:

我正在使用Hackney's erlang rest 客户端。我按照 README.md 中提供的步骤进行操作,但出现以下错误:

17> Method = get.
get
18> URL = <<"www.google.com">>.
<<"www.google.com">>
19> Headers = [].
[]
20> Payload = <<>>.
<<>>
21> Options = [].
[]
22>Test  = hackney:request(Method, URL,Headers,Payload,Options).
{error,connect_timeout} 

我使用 curl 和 wget 使用了相同的 url,两者都在工作。 erlang ssl 或 tls 有问题吗?我已经编辑了这个问题以便更好地理解

编辑 1(使用 curl -vv google.com)

curl -vv google.com
* About to connect() to proxy <<ip>> port 8080 (#0)
*   Trying <<ip>>... connected
* Connected to <<ip>> (<<ip>>) port 8080 (#0)
* Proxy auth using Basic with user '<<user>>'
> GET http://google.com HTTP/1.1
> Proxy-Authorization: <<proxy authorization>>
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: google.com
> Accept: */*
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Tue, 07 Jun 2016 03:49:43 GMT
< Expires: Thu, 07 Jul 2016 03:49:43 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Proxy-Connection: Keep-Alive
< Connection: Keep-Alive
< Age: 2223
<
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
* Connection #0 to host <<ip>> left intact
* Closing connection #0

【问题讨论】:

    标签: erlang


    【解决方案1】:

    Hackney 不会自动应用配置文件代理设置,因此您应该自己处理代理设置。

    根据documentation,您应该提供以下选项:

    {proxy, {Host, Port}} %% if http proxy is used
    {proxy_auth, {User, Password}}. %% if proxy requires authentication 
    

    【讨论】:

      【解决方案2】:

      当您使用 httpc 模块通过 Erlang shell 发出请求时,您会得到什么。 首先启动 inets:

      inets:start().
      

      那就试试吧:

      {ok, Response} = httpc:request("https://www.google.com").
      

      {ok, Response} = httpc:request("http://www.google.com").
      

      如果这两个都无法连接,则问题可能与 hackney 无关,而是整个 Erlang 的问题。

      【讨论】:

      • 我收到以下消息:** 异常错误:右侧值不匹配 {error,{failed_connect,[{to_address,{"www.google.com",443}}, {inet,[inet],etimedout}]}} 为他们两个
      • 那么你的操作系统和 Erlang 有问题,这个问题很可能与 Erlang 无关。
      • 我可以使用 wget 和 curl 访问相同的 url...我猜是 erlang 的问题
      • @SujaySudeep,你使用任何代理吗?告诉我curl -vv google.com
      • @Lol4t0 请检查您提供的命令的结果
      【解决方案3】:

      您的错误不是connect_timeout。您收到了 no match of right hand side value 的异常,因为您在最后一个命令中缺少 =
      改成

      {ok, StatusCode, RespHeaders, ClientRef} = hackney:request(Method,URL,Headers,Payload,Options).
      

      【讨论】:

      • 嗨 Ohad ..这是在 Stackoverflow 中复制代码时出现的问题..让我们让它变得简单.. Test = hackney:request(Method,URL,Headers,Payload,Options)。它给出以下错误:{error,connect_timeout}
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      相关资源
      最近更新 更多