【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.
posted on 2012-09-05 21:30  Anthony Li  阅读(3431)  评论(0编辑  收藏  举报

博客园博客已停止更新,博客地址:dyinigbleed.com