Using inets http client
Ingela Anderton
ingela@REDACTED
Tue Jun 7 12:31:23 CEST 2005
Sorry for not being quite as quick to answer as Michael but yesterday
was a new Swedish national holiday, so I have not read my mail
since Friday.
What you are doing wrong is that your functions below is not
tail-recursive. An erlang function always returns the value
of its last statement. So the first time around you do receive
the response as you expect but instead of returning it
it will be thrown away and you call http_wait_with_timeout again!
Try writing your function like this instead:
http_wait_with_timeout(RequestId, Timeout) ->
if
Timeout > 0 ->
receive
{http, {RequestId, Result}} ->
Result;
{http, {RequestId, {error, Reason}}} ->
throw({error, Reason})
after 1000 ->
http_wait_with_timeout(RequestId, Timeout - 1000)
end;
true ->
throw({timeout})
end.
By the way are you aware that you can let the http-client handle
the timeout part and if the timeout occurs your process
will receive the message {http, RequestId, {error, timeout}}!
Call to the client would look like this:
http:request(get, {Url, []}, [{timeout, 10000}], [{sync, false}])
Will Newton wrote:
>
> Following the documentation of the inets http client I have written this code:
>
> http_wait_with_timeout(RequestId, Timeout) ->
> if
> Timeout > 0 ->
> receive
> {http, {RequestId, Result}} ->
> Result;
> {http, {RequestId, {error, Reason}}} ->
> throw({error, Reason})
> after
> 1000 ->
> io:format(".")
> end,
> http_wait_with_timeout(RequestId, Timeout - 1000);
> true ->
> throw({timeout})
> end.
>
> get_url(Url) ->
> application:start(inets),
> case http:request(get, {Url, []}, [], [{sync, false}]) of
> {ok, RequestId} ->
> http_wait_with_timeout(RequestId, 15000);
> _ ->
> io:format("http request failed~n")
> end.
>
> I get a series of periods printed but I never get the expected response from
> the async http request. Can anyone tell me what am I doing wrong?
>
> Thanks,
--
/Ingela - OTP team
More information about the erlang-questions
mailing list