HTTP: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Undo revision 315433 by WdeCvfYlmB (talk)) |
Thundergnat (talk | contribs) (Undo revision 315432 by WdeCvfYlmB (talk)) |
||
Line 807: | Line 807: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
===Synchronous=== |
===Synchronous=== |
||
<lang erlang> |
<lang erlang> |
||
-module(main). |
|||
-export([main/1]). |
-export([main/1]). |
||
Line 815: | Line 816: | ||
{ok, {_V, _H, Body}} -> io:fwrite("~p~n",[Body]); |
{ok, {_V, _H, Body}} -> io:fwrite("~p~n",[Body]); |
||
{error, Res} -> io:fwrite("~p~n", [Res]) |
{error, Res} -> io:fwrite("~p~n", [Res]) |
||
end. |
end. |
||
⚫ | |||
===Asynchronous=== |
===Asynchronous=== |
||
<lang erlang> |
<lang erlang> |
||
-module(main). |
|||
-export([main/1]). |
-export([main/1]). |
||
main([Url|[]]) -> |
main([Url|[]]) -> |
||
inets:start(), |
inets:start(), |
||
Line 826: | Line 831: | ||
_Any -> io:fwrite("Error: ~p~n",[_Any]) |
_Any -> io:fwrite("Error: ~p~n",[_Any]) |
||
after 10000 -> io:fwrite("Timed out.~n",[]) |
after 10000 -> io:fwrite("Timed out.~n",[]) |
||
end. |
end. |
||
⚫ | |||
Using it |
Using it |
||
⚫ | |||
⚫ | |||
⚫ | |||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
In F# we can just use the .NET library to do this so its the same as the [[C_sharp|C#]] example. |
In F# we can just use the .NET library to do this so its the same as the [[C_sharp|C#]] example. |
||
⚫ | |||
⚫ | |||
⚫ | |||
use c = new System.Net.WebClient() |
use c = new System.Net.WebClient() |
||
c.DownloadString(url) |
c.DownloadString(url) |
||
⚫ | |||
⚫ | |||
⚫ | |||
However unlike C#, F# can use an asynchronous workflow to avoid blocking any threads while waiting for a response from the server. To asynchronously download three url's at once... |
However unlike C#, F# can use an asynchronous workflow to avoid blocking any threads while waiting for a response from the server. To asynchronously download three url's at once... |
||
⚫ | |||
⚫ | |||
⚫ | |||
open System.IO |
open System.IO |
||
⚫ | |||
let request = WebRequest.Create (url:string) |
|||
⚫ | |||
use! response = request.AsyncGetResponse() |
|||
use responseStream = response.GetResponseStream() |
|||
use reader = new StreamReader(responseStream) |
|||
use responseStream = response.GetResponseStream() |
|||
return reader.ReadToEnd() |
|||
} |
|||
return reader.ReadToEnd() } |
|||
let urls = ["http://www.w3.org/Home.html"] |
|||
let urls = ["http://www.rosettacode.org/"; "http://www.yahoo.com/"; "http://www.google.com/"] |
|||
let content = urls |
let content = urls |
||
|> List.map wgetAsync |
|> List.map wgetAsync |