Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL cUrl
LOCAL oResp
LOCAL nRemoteFileSize
LOCAL nChunkSize
LOCAL oHttp1
LOCAL oHttp2
LOCAL oHttp3
LOCAL oHttp4
LOCAL oSbRange
LOCAL nNumReplaced
LOCAL oTask1
LOCAL oTask2
LOCAL oTask3
LOCAL oTask4
LOCAL nNumLive
LOCAL nNumErrors
LOCAL oFac
LOCAL nBSame

nSuccess := 0

//  This requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oHttp := CreateObject("Chilkat.Http")

//  First get the size of the file to be downloaded.
cUrl := "https://www.chilkatsoft.com/hamlet.xml"

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpNoBody("HEAD", cUrl, oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oResp:destroy()
    RETURN
ENDIF

nRemoteFileSize := oResp:ContentLength

? "Downloading " + Str(nRemoteFileSize) + " bytes..."

//  Let's download in 4 chunks.
//  (the last chunk will be whatever remains after the 1st 3 equal sized chunks)
nChunkSize := nRemoteFileSize / 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 := CreateObject("Chilkat.Http")
oHttp2 := CreateObject("Chilkat.Http")
oHttp3 := CreateObject("Chilkat.Http")
oHttp4 := CreateObject("Chilkat.Http")

oSbRange := CreateObject("Chilkat.StringBuilder")
oSbRange:SetString("bytes=<range-start>-<range-end>")
nNumReplaced := oSbRange:ReplaceI("<range-start>", 0)
nNumReplaced := oSbRange:ReplaceI("<range-end>", nChunkSize - 1)
? oSbRange:GetAsString()
oHttp1:SetRequestHeader("Range", oSbRange:GetAsString())

oSbRange:SetString("bytes=<range-start>-<range-end>")
nNumReplaced := oSbRange:ReplaceI("<range-start>", nChunkSize)
nNumReplaced := oSbRange:ReplaceI("<range-end>", 2 * nChunkSize - 1)
? oSbRange:GetAsString()
oHttp2:SetRequestHeader("Range", oSbRange:GetAsString())

oSbRange:SetString("bytes=<range-start>-<range-end>")
nNumReplaced := oSbRange:ReplaceI("<range-start>", 2 * nChunkSize)
nNumReplaced := oSbRange:ReplaceI("<range-end>", 3 * nChunkSize - 1)
? oSbRange:GetAsString()
oHttp3:SetRequestHeader("Range", oSbRange:GetAsString())

oSbRange:SetString("bytes=<range-start>-")
nNumReplaced := oSbRange:ReplaceI("<range-start>", 3 * nChunkSize)
? oSbRange:GetAsString()
oHttp4:SetRequestHeader("Range", oSbRange:GetAsString())

//  Start each range download
oTask1 := oHttp1:DownloadAsync(cUrl, "qa_output/chunk1.dat")
oTask1:Run()

oTask2 := oHttp2:DownloadAsync(cUrl, "qa_output/chunk2.dat")
oTask2:Run()

oTask3 := oHttp3:DownloadAsync(cUrl, "qa_output/chunk3.dat")
oTask3:Run()

oTask4 := oHttp4:DownloadAsync(cUrl, "qa_output/chunk4.dat")
oTask4:Run()

//  Wait for the downloads to complete.
nNumLive := 4
DO WHILE nNumLive > 0
    nNumLive := 0
    IF (oTask1:Live == 1)
        nNumLive := nNumLive + 1
    ENDIF

    IF (oTask2:Live == 1)
        nNumLive := nNumLive + 1
    ENDIF

    IF (oTask3:Live == 1)
        nNumLive := nNumLive + 1
    ENDIF

    IF (oTask4:Live == 1)
        nNumLive := nNumLive + 1
    ENDIF

    IF (nNumLive > 0)
        //  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

ENDDO

//  All should be downloaded now..
//  Examine the result of each Download.
nNumErrors := 0
IF (oTask1:GetResultBool() == 0)
    ? oTask1:ResultErrorText
    nNumErrors := nNumErrors + 1
ENDIF

IF (oTask2:GetResultBool() == 0)
    ? oTask2:ResultErrorText
    nNumErrors := nNumErrors + 1
ENDIF

IF (oTask3:GetResultBool() == 0)
    ? oTask3:ResultErrorText
    nNumErrors := nNumErrors + 1
ENDIF

IF (oTask4:GetResultBool() == 0)
    ? oTask4:ResultErrorText
    nNumErrors := nNumErrors + 1
ENDIF

IF (nNumErrors > 0)
    oTask1:destroy()
    oTask2:destroy()
    oTask3:destroy()
    oTask4:destroy()
    oHttp:destroy()
    oResp:destroy()
    oHttp1:destroy()
    oHttp2:destroy()
    oHttp3:destroy()
    oHttp4:destroy()
    oSbRange:destroy()
    RETURN
ENDIF

//  All downloads were successful.
//  Compose the file from the parts.
oFac := CreateObject("Chilkat.FileAccess")
nSuccess := oFac:ReassembleFile("qa_output", "chunk", "dat", "qa_output/hamlet.xml")
IF (nSuccess == 0)
    ? oFac:LastErrorText
ELSE
    ? "Success."
ENDIF

oTask1:destroy()
oTask2:destroy()
oTask3:destroy()
oTask4:destroy()

//  Let's download in the regular way, and then compare files..
nSuccess := oHttp:Download(cUrl, "qa_output/hamletRegular.xml")

//  Compare files.
nBSame := oFac:FileContentsEqual("qa_output/hamlet.xml", "qa_output/hamletRegular.xml")
? "bSame = " + Str(nBSame)

oHttp:destroy()
oResp:destroy()
oHttp1:destroy()
oHttp2:destroy()
oHttp3:destroy()
oHttp4:destroy()
oSbRange:destroy()
oFac:destroy()