#PSTip Test if a download can be suspended and resumed

Today, most of the web servers that provide downloads enable resumable downloads. Of course, there are still sites that don’t offer resumable downloads. When using BITS transfer cmdlets, we can suspend and resume downloads using the Suspend-BitsTransfer and Resume-BitsTransfer cmdlets. However, if the web server does not support resumable downloads, trying to resume a download using BITS transfer cmdlets will have no impact.

This is where we can inspect the HTTP response headers to test if a server supports resuming downloads. The HTTP header that tells us this information is “Accept-Ranges“. The value of this header will be set to none if the server does not supporting resuming downloads. The following function helps us in testing for resumable downloads.

Function Test-ResumableDownload {
    param (
       [String]$url
    )
    $request = [System.Net.WebRequest]::Create($url)
    $request.Method = "GET"
    $result = $request.GetResponse()
    $result.Headers["Accept-Ranges"] -ne "none"
}

We can use this technique as a prerequisite before starting a download using the Start-BitsTransfer cmdlet to let the user know whether a download can be resumed or not.

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: