Skip to content

WebClient needs to expose the timeout property (or improve Invoke-WebRequest implementation) when downloading large files  #4679

Closed

Description

@swinster

I have been having some issues interacting with REST full APIs, specifically with our product (Pexip) which is writing with Python and Django and uses Apache. Some of these issues are outlined in #2112 and #4274 and this involves a similar an linked problem. I also added a comment to https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-5.1 that outlines this issue that I have repeated below:

Attempting to download a large file, GETting from a REST API.

The Basic Invoke-WebRequest

    Invoke-WebRequest -Method Get -Uri $resource -cred $cred -OutFile $Destination -TimeoutSec 1800

This would be the ideal. I can set timeout values and seemingly achieve what I want in one line. Unfortunately, this completely locked up the machine the script was running on. Basically, all the memory was consumed and after an hour or so, I had to end the script – of the 3.5 GB file, I think 200 MB was downloaded! What I needed is a way to stream the remote file to a local file without loading everything into memory.

Using WebClient

This seemed a lot better, however, the WebClient has no timeout property!!!

    $wc = New-Object System.Net.WebClient     
    $wc.Credentials = New-Object System.Net.NetworkCredential($User, $Pass)
    $wc.Headers.Add("Authorization", "Basic $encoded")
    $wc.DownloadFile($resource, $DestinationPath)

This is what I ended up using and in order to get the timeout to work, I used an extended class – luckily someone clever than I has achieved this for PowerShell using a C# class (https://koz.tv/setup-webclient-timeout-in-powershell/) although I assume the native classes in PS could also be used – but why?

I have also found that I must add the additional Authorisation Headers, otherwise the client will get challenged then end up download the file effectively twice. Using Fiddler and watching the Ethernet stats, you can see this happen. Indeed, this seemingly occurred for all methods

Using HTTP client.

Well I tried and failed dismally. However, I seem to create the Httpclient, it only ever seemed to stream the remote file into memory before dumping to a file. I know what I wanted to do, but do not have the wherewithal to do it.

Activity

  1. added this to the milestone on Aug 26, 2017
  2. iSazonov commented on May 26, 2018

    @iSazonov
    Collaborator

    Mark Kraus (@markekraus) Is the Issue already fixed?

  3. markekraus commented on May 26, 2018

    @markekraus
    Contributor

    No. we added a resume feature but not a way to timeoul a download.

    This is, however, something we could add. Since we do a looped read of the stream, we could insert logic in their to stop when the time out is reached.

  4. iSazonov commented on May 27, 2018

    @iSazonov
    Collaborator

    Ah, I again confused these two PR.

  5. CarloToso commented on Mar 15, 2023

    @CarloToso
    Contributor

    stevenebutler This issue can be fixed with the -ReceiveTimeout (the name is a placeholder) parameter discussed in #19315. I think you could fix this issue after we merge #19330

  6. iSazonov commented on Mar 16, 2023

    @iSazonov
    Collaborator

    This issue touches several problems but the main request is "What I needed is a way to stream the remote file to a local file without loading everything into memory."

    Does this problem still exist and in what specific scenarios?

  7. CarloToso commented on Mar 16, 2023

    @CarloToso
    Contributor

    There are two problems here:

    • Download streaming. No longer an issue, Test: Invoke-WebRequest https://speed.hetzner.de/10GB.bin -OutFile 10gb.bin
      (it still writes to memory if we use -PassThru, but I think it's by design)
    • A timeout during the download. Can be addressed with a -ReceiveTimeout property
  8. iSazonov commented on Mar 16, 2023

    @iSazonov
    Collaborator
    • Download streaming. No longer an issue, Test: Invoke-WebRequest https://speed.hetzner.de/10GB.bin -OutFile 10gb.bin
      (it still writes to memory if we use -PassThru, but I think it's by design)

    If so we should close the issue.

    • A timeout during the download. Can be addressed with a -ReceiveTimeout property

    It is another request. I think it makes no sense. Otherwise we will have to add timeouts in all cmdlets like Copy-Item, Get-Content and so on. If you want to discuss this please open new issue.

  9. ghost closed this as completedon Mar 17, 2023
  10. andrei-epure-sonarsource commented on Aug 17, 2023

    @andrei-epure-sonarsource

    CarloToso starting what version does the download streaming work by design, please?

    Thanks!

  11. CarloToso commented on Aug 17, 2023

    @CarloToso
    Contributor

    I tested it works correctly in 5.1, 7.2, 7.3, 7.4. I hypotesize it was fixed before 6.1, but I didn't check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions