我开发了这个功能:
sms_sentByHttp(NumPhone,MsgReceived) ->
NumPhoneTroncated = string:sub_string(NumPhone,2),
{X1, X2, X3} = erlang:now(),
Rand = random:uniform(9999),
{{Y,M,D},{H,Mn,S}}=erlang:universaltime(),
Ws_req_id = lists:flatten(io_lib:format("~p~p~p~p~p~p~p~p", [X3, Rand,Y,M,D,H,M,S])),
Url = io_lib:format("http://localhost:7703/enda?msg=~s&from=~s&id=~s", [http_urii:encode(MsgReceived),http_urii:encode(NumPhoneTroncated),Ws_req_id]),
case http:request(lists:flatten(Url)) of
{ok , A} -> io:format("response sent\n");
{error, B} -> io:format("response not sent\n ~w\n", [B])
end.
现在我想在请求中添加超时的概念,例如 20 秒后。我想显示来自服务器的错误
我试过:
case http:request(lists:flatten(Url),[ {timeout,timer:seconds(20)}]) of
{ok , A} -> io:format("response sent\n");
{error, B} -> io:format("error from server\n ~w\n", [B])
end.