使用Hackney时获取{error,connect_timeout}消息

时间:2016-06-06 10:09:36

标签: erlang

我正在使用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

3 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

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

inets:start().

然后尝试:

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

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

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

答案 2 :(得分:0)

您的错误不是connect_timeout。您将获得no match of right hand side value的例外,因为您错过了上一个命令的= 只需将其更改为

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