open System
open System.Net
let fetchUrlAsync url =
async {
Console.WriteLine(sprintf "Fetch <%s>" url)
let req = WebRequest.Create(Uri(url))
use! resp = req.AsyncGetResponse()
use stream = resp.GetResponseStream()
use reader = new IO.StreamReader(stream)
let html = reader.ReadToEnd()
Console.WriteLine(sprintf "finished downloading %s. Length = %i" url html.Length)
}
[<EntryPoint>]
let main argv =
["http://bing.com"; "http://ya.ru"; "http://google.com"]
|> List.map fetchUrlAsync
|> Async.Sequential
|> Async.Ignore
|> Async.RunSynchronously
Run Code Online (Sandbox Code Playgroud)
输出:
Fetch <http://bing.com>
Fetch <http://ya.ru>
Fetch <http://google.com>
finished downloading http://google.com. Length = 50592
finished downloading http://ya.ru. Length = …Run Code Online (Sandbox Code Playgroud)