Last active
November 7, 2019 21:42
-
-
Save curtisalexander/7b4cb1b6c6fc9fe583aee9473766c899 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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