Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

AutoIt Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(AutoIt) HTTP Download in Parallel with Simultaneous Range Requests

Demonstrates how to download a large file with parallel simultaneous requests, where each request downloads a segment (range) of the remote file.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

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

$oHttp = ObjCreate("Chilkat.Http")
Local $bSuccess

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

Local $oResp = $oHttp.GetHead($sUrl)
If ($oHttp.LastMethodSuccess = 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)

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.