如何在Erlang中进行HTTPS请求?

25

我尝试使用inets库,但它超时了。我不认为它支持HTTPS。我正在尝试使用ibrowse,但它没有起作用。


7
“它不起作用了?”发布你的错误!发布你的代码! - Jim Puls
4个回答

31

这对我来说很好用:

1> application:start(inets).
ok
2> application:start(ssl).  
ok
3> http:request(head, {"https://example.com", []}, [{ssl,[{verify,0}]}], []).
{ok,{{"HTTP/1.1",200,"OK"},
     [{"cache-control","max-age=0, proxy-revalidate"},
      {"date","Sun, 23 May 2010 00:38:33 GMT"},
      {"server","BAIDA/1.0.0"},
      {"content-type","text/html; charset=windows-1251"},
      {"expires","Sun, 23 May 2010 00:38:33 GMT"},
      {"set-cookie",
       "uid=9041986921274575113; domain=.example.com; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT"}],
     []}}

http:request("https://example.com") 也可以工作,但您必须在任何请求之前加载适当的应用程序。


3
这是我用过的方法:
application:start(crypto),
application:start(public_key),
application:start(ssl),
application:start(inets).

httpc:request(head, {"https://example.com", []}, [{ssl,[{verify,0}]}], []).

2
在 Erlang/OTP 25 中,这对我起作用:
httpc:request(get, {"https://erlang.com", []}, [{ssl, [{verify, verify_peer}, {cacerts, public_key:cacerts_get()}]}], []).

0

对于我来说,在启用了对等验证的情况下,这在 Erlang/OTP 24 上的 Yaws 2.1.0 中适用于 GET 请求:

application:start(inets).
application:start(crypto).
application:start(asn1).
application:start(public_key).
application:start(ssl).

httpc:request(get, {"https://example.com", []}, 
    [{ssl, [{verify, verify_peer}, {cacertfile,"/path/to/cacertfile.crt"}]}], []).

否则将会出现警告:"证书路径验证未建立真实性" 更多选项请参见:https://www.erlang.org/doc/man/httpc.html#request-4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接