Xbase++
Xbase++
Bunny Sign URL and then Download using Signed URL
See more Bunny CDN Examples
Shows how to sign a URL for BunnyCDN Token Authentication and then use it to download.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL cMySecurityKey
LOCAL cUrl
LOCAL oUrlObj
LOCAL cUrl_scheme
LOCAL cUrl_host
LOCAL cUrl_path
LOCAL oExpTime
LOCAL cExpires
LOCAL oSbToHash
LOCAL cToken
LOCAL oSbSignedUrl
LOCAL cSignedUrl
LOCAL oHttp
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
cMySecurityKey := "e7ea8d73-8fa0-44ef-a2cc-91526f7df5ed"
cUrl := "https://test.b-cdn.net/sample-pdf-with-images.pdf"
// Extract the URL components.
oUrlObj := CreateObject("Chilkat.Url")
oUrlObj:ParseUrl(cUrl)
cUrl_scheme := "https"
IF (oUrlObj:Ssl == 0)
cUrl_scheme := "http"
ENDIF
cUrl_host := oUrlObj:Host
cUrl_path := oUrlObj:Path
// Calculate an expiration time 1 hour from the current date/time.
oExpTime := CreateObject("Chilkat.CkDateTime")
oExpTime:SetFromCurrentSystemTime()
oExpTime:AddSeconds(3600)
cExpires := oExpTime:GetAsUnixTimeStr(0)
? "Expires = " + cExpires
// Create the string to hash
oSbToHash := CreateObject("Chilkat.StringBuilder")
oSbToHash:Append(cMySecurityKey)
oSbToHash:Append(cUrl_path)
oSbToHash:Append(cExpires)
// Base64Url encoding is the same as base64, except "-" is used instead of "+",
// "_" is used instead of "/", and no "=" padding is added.
cToken := oSbToHash:GetHash("sha256", "base64Url", "utf-8")
oSbSignedUrl := CreateObject("Chilkat.StringBuilder")
oSbSignedUrl:Append(cUrl_scheme)
oSbSignedUrl:Append("://")
oSbSignedUrl:Append(cUrl_host)
oSbSignedUrl:Append(cUrl_path)
oSbSignedUrl:Append("?token=")
oSbSignedUrl:Append(cToken)
oSbSignedUrl:Append("&expires=")
oSbSignedUrl:Append(cExpires)
cSignedUrl := oSbSignedUrl:GetAsString()
? "Signed URL: " + cSignedUrl
// Use the signed URL to download the file.
oHttp := CreateObject("Chilkat.Http")
nSuccess := oHttp:Download(cSignedUrl, "c:/aaworkarea/sample.pdf")
IF (nSuccess == 0)
? oHttp:LastErrorText
ELSE
? "Success."
ENDIF
oUrlObj:destroy()
oExpTime:destroy()
oSbToHash:destroy()
oSbSignedUrl:destroy()
oHttp:destroy()