HTTP: Difference between revisions

Content added Content deleted
(Undo revision 315433 by WdeCvfYlmB (talk))
(Undo revision 315432 by WdeCvfYlmB (talk))
Line 807: Line 807:
=={{header|Erlang}}==
=={{header|Erlang}}==
===Synchronous===
===Synchronous===
<lang erlang>-module(main).
<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.</lang>
end.
</lang>

===Asynchronous===
===Asynchronous===
<lang erlang>-module(main).
<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.</lang>
end.
</lang>

Using it
Using it
<lang erlang>
<lang erlang>|escript ./req.erl http://www.w3.org/Home.html</lang>
|escript ./req.erl http://www.rosettacode.org
</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.
<lang fsharp>let wget (url : string) =

<lang fsharp>
let wget (url : string) =
use c = new System.Net.WebClient()
use c = new System.Net.WebClient()
c.DownloadString(url)
c.DownloadString(url)
printfn "%s" (wget "http://www.w3.org/Home.html")</lang>

printfn "%s" (wget "http://www.rosettacode.org/")
</lang>

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...
<lang fsharp>open System.Net

<lang fsharp>
open System.Net
open System.IO
open System.IO
let wgetAsync url = async {

let request = WebRequest.Create (url:string)
let wgetAsync url =
async { let request = WebRequest.Create (url:string)
use! response = request.AsyncGetResponse()
use! response = request.AsyncGetResponse()
use responseStream = response.GetResponseStream()
use reader = new StreamReader(responseStream)
use responseStream = response.GetResponseStream()
use reader = new StreamReader(responseStream)
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