【Erlang新手成长日记】HTTP客户端


1、启动

方式一:启动inets应用,一个缺省profile的管理进程将被启动。

inets:start().

方式二:运行时,动态启动profile停止profile。

动态启动profile:

{ok, Pid} = inets:start(httpc, [{profile, foo}]).

动态停止profile:

inets:stop(httpc, foo).

inets:stop(httpc, Pid).

 

2、设置

httpc:set_options() -> ok | {error, Reason}

参考:http://www.erlang.org/doc/man/httpc.html#set_options-1

 

3、请求

参考:http://www.erlang.org/doc/man/httpc.html#request-1

同步请求:

{ok, {{Version, 200, ReasonPhrase}, Headers, Body}} = httpc:request("http://www.baidu.com").

等同于

{ok, {{Version, 200, ReasonPhrase}, Headers, Body}} = httpc:request(get, {"http://www.baidu.com", []}, [], []).

异步请求:

{ok, RequestId} = httpc:request(get, {"http://www.baidu.com", []}, [], [{sync, false}]),
receive {http, {RequestId, Result}} -> ok after 500 -> error end.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM