Skip to content

Instantly share code, notes, and snippets.

@curtisalexander
Last active November 7, 2019 21:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
open System.Net.Http
open System.IO
let downloadDataWithHttpClient (url: string) =
async {
use hc = new HttpClient()
// Just read the header
let! response = hc.GetAsync(url, HttpCompletionOption.ResponseHeadersRead) |> Async.AwaitTask
// if response.IsSuccessStatusCode
let filename = Path.GetTempFileName()
printfn "Starting download to file %s" filename
let! streamToReadFrom = response.Content.ReadAsStreamAsync() |> Async.AwaitTask
use streamToWriteTo = File.Open(filename, FileMode.Create)
do! streamToReadFrom.CopyToAsync(streamToWriteTo) |> Async.AwaitTask
return ()
}
let downloadData =
"http://fsharp.org"
|> downloadDataWithHttpClient
|> Async.RunSynchronously
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment