> let req = WebRequest.Create("http://www.microsoft.com");; val req : WebRequest > let resp = req.GetResponse();; val resp : WebResponse > let stream = resp.GetResponseStream();; val stream : Stream > let reader = new StreamReader(stream) ;; val reader : StreamReader > let html = reader.ReadToEnd();; val html : string = "..."
However, writing codes in such way would be too imperative. By using the idiomatic using function, we can write it in a more concise and safer form:
#light let http (url: string) = let req = WebRequest.Create(url) using (req.GetResponse()) (fun res -> using (res.GetResponseStream()) (fun stream -> let reader = new StreamReader(stream) reader.ReadToEnd()))
The arguments of using are:
val it : 'a -> ('a -> 'b) -> 'b when 'a :> IDisposable =
Clearly, the first argument requires a resource that implements the IDisposable interface ( :> is a special operator in F# for up-casting); then using will feed the resource to a function to process further and finally will guarantee to release the resource when everything is done.