Skip to content

Instantly share code, notes, and snippets.

@curtisalexander
Last active November 7, 2019 21:42
Show Gist options
  • Save curtisalexander/7b4cb1b6c6fc9fe583aee9473766c899 to your computer and use it in GitHub Desktop.
Save curtisalexander/7b4cb1b6c6fc9fe583aee9473766c899 to your computer and use it in GitHub Desktop.

Revisions

  1. curtisalexander revised this gist Nov 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fsharp-httpclient.fsx
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ let downloadDataWithHttpClient (url: string) =
    return ()
    }

    let downloadData2 =
    let downloadData =
    "http://fsharp.org"
    |> downloadDataWithHttpClient
    |> Async.RunSynchronously
  2. curtisalexander created this gist Nov 7, 2019.
    21 changes: 21 additions & 0 deletions fsharp-httpclient.fsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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 downloadData2 =
    "http://fsharp.org"
    |> downloadDataWithHttpClient
    |> Async.RunSynchronously