AutoIt
AutoIt
HTTP Download in Parallel with Simultaneous Range Requests
See more HTTP Examples
Demonstrates how to download a large file with parallel simultaneous requests, where each request downloads a segment (range) of the remote file.Chilkat AutoIt Downloads
Local $bSuccess = False
; This requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
$oHttp = ObjCreate("Chilkat.Http")
; First get the size of the file to be downloaded.
Local $sUrl = "https://www.chilkatsoft.com/hamlet.xml"
$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpNoBody("HEAD",$sUrl,$oResp)
If ($bSuccess = False) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Exit
EndIf
Local $iRemoteFileSize = $oResp.ContentLength
ConsoleWrite("Downloading " & $iRemoteFileSize & " bytes..." & @CRLF)
; Let's download in 4 chunks.
; (the last chunk will be whatever remains after the 1st 3 equal sized chunks)
Local $iChunkSize = $iRemoteFileSize / 4
; The Range header is used to download a range from a resource
; Range: bytes=<range-start>-<range-end>
; or
; Range: bytes=<range-start>-
; We're writing code this way for clarity..
$oHttp1 = ObjCreate("Chilkat.Http")
$oHttp2 = ObjCreate("Chilkat.Http")
$oHttp3 = ObjCreate("Chilkat.Http")
$oHttp4 = ObjCreate("Chilkat.Http")
$oSbRange = ObjCreate("Chilkat.StringBuilder")
$oSbRange.SetString("bytes=<range-start>-<range-end>")
Local $iNumReplaced = $oSbRange.ReplaceI("<range-start>",0)
$iNumReplaced = $oSbRange.ReplaceI("<range-end>",$iChunkSize - 1)
ConsoleWrite($oSbRange.GetAsString() & @CRLF)
$oHttp1.SetRequestHeader "Range",$oSbRange.GetAsString()
$oSbRange.SetString("bytes=<range-start>-<range-end>")
$iNumReplaced = $oSbRange.ReplaceI("<range-start>",$iChunkSize)
$iNumReplaced = $oSbRange.ReplaceI("<range-end>",2 * $iChunkSize - 1)
ConsoleWrite($oSbRange.GetAsString() & @CRLF)
$oHttp2.SetRequestHeader "Range",$oSbRange.GetAsString()
$oSbRange.SetString("bytes=<range-start>-<range-end>")
$iNumReplaced = $oSbRange.ReplaceI("<range-start>",2 * $iChunkSize)
$iNumReplaced = $oSbRange.ReplaceI("<range-end>",3 * $iChunkSize - 1)
ConsoleWrite($oSbRange.GetAsString() & @CRLF)
$oHttp3.SetRequestHeader "Range",$oSbRange.GetAsString()
$oSbRange.SetString("bytes=<range-start>-")
$iNumReplaced = $oSbRange.ReplaceI("<range-start>",3 * $iChunkSize)
ConsoleWrite($oSbRange.GetAsString() & @CRLF)
$oHttp4.SetRequestHeader "Range",$oSbRange.GetAsString()
; Start each range download
Local $oTask1 = $oHttp1.DownloadAsync($sUrl,"qa_output/chunk1.dat")
$oTask1.Run()
Local $oTask2 = $oHttp2.DownloadAsync($sUrl,"qa_output/chunk2.dat")
$oTask2.Run()
Local $oTask3 = $oHttp3.DownloadAsync($sUrl,"qa_output/chunk3.dat")
$oTask3.Run()
Local $oTask4 = $oHttp4.DownloadAsync($sUrl,"qa_output/chunk4.dat")
$oTask4.Run()
; Wait for the downloads to complete.
Local $iNumLive = 4
While $iNumLive > 0
$iNumLive = 0
If ($oTask1.Live = True) Then
$iNumLive = $iNumLive + 1
EndIf
If ($oTask2.Live = True) Then
$iNumLive = $iNumLive + 1
EndIf
If ($oTask3.Live = True) Then
$iNumLive = $iNumLive + 1
EndIf
If ($oTask4.Live = True) Then
$iNumLive = $iNumLive + 1
EndIf
If ($iNumLive > 0) Then
; SleepMs is a convenience method to cause the caller to sleep for N millisec.
; It does not cause the given task to sleep..
$oTask1.SleepMs 10
EndIf
Wend
; All should be downloaded now..
; Examine the result of each Download.
Local $iNumErrors = 0
If ($oTask1.GetResultBool() = False) Then
ConsoleWrite($oTask1.ResultErrorText & @CRLF)
$iNumErrors = $iNumErrors + 1
EndIf
If ($oTask2.GetResultBool() = False) Then
ConsoleWrite($oTask2.ResultErrorText & @CRLF)
$iNumErrors = $iNumErrors + 1
EndIf
If ($oTask3.GetResultBool() = False) Then
ConsoleWrite($oTask3.ResultErrorText & @CRLF)
$iNumErrors = $iNumErrors + 1
EndIf
If ($oTask4.GetResultBool() = False) Then
ConsoleWrite($oTask4.ResultErrorText & @CRLF)
$iNumErrors = $iNumErrors + 1
EndIf
If ($iNumErrors > 0) Then
Exit
EndIf
; All downloads were successful.
; Compose the file from the parts.
$oFac = ObjCreate("Chilkat.FileAccess")
$bSuccess = $oFac.ReassembleFile("qa_output","chunk","dat","qa_output/hamlet.xml")
If ($bSuccess = False) Then
ConsoleWrite($oFac.LastErrorText & @CRLF)
Else
ConsoleWrite("Success." & @CRLF)
EndIf
; Let's download in the regular way, and then compare files..
$bSuccess = $oHttp.Download($sUrl,"qa_output/hamletRegular.xml")
; Compare files.
Local $bSame = $oFac.FileContentsEqual("qa_output/hamlet.xml","qa_output/hamletRegular.xml")
ConsoleWrite("bSame = " & $bSame & @CRLF)