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

PowerBuilder 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

 

 

 

(PowerBuilder) REST Download Bandwidth Throttle

Demonstrates how to use download bandwidth throttling with the REST API. This example will download a file from Drobox using a file stream, with a limit on the bandwidth that can be used for the transfer.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
oleobject loo_Socket
integer li_MaxWaitMs
integer li_Success
oleobject loo_Rest
oleobject loo_Json
oleobject loo_FileStream
integer li_ExpectedStatus
string ls_ResponseStr
string ls_ApiResult
oleobject loo_JsonResult
integer li_Size
string ls_Rev
string ls_ClientModified
oleobject loo_Ckdt
integer li_BLocalTime
oleobject loo_Dt

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

// A Dropbox access token should have been previously obtained.
// Dropbox access tokens do not expire.
// See Dropbox Access Token.

// To use bandwidth throttling, the connection should be made using the socket API.
// This provides numerous properties to customize the connection, such as
// BandwidthThrottleDown, BandwidthThrottleUp, ClientIpAddress, ClintPort, Http Proxy,
// KeepAlive, PreferIpv6, RequireSslCertVerify, SoRcvBuf, SoSndBuf, SoReuseAddr,
// SOCKS proxy, TcpNoSDelay, TlsPinSet, TlsCipherSuite, SslAllowedCiphers, etc.

loo_Socket = create oleobject
// Use "Chilkat_9_5_0.Socket" for versions of Chilkat < 10.0.0
li_rc = loo_Socket.ConnectToNewObject("Chilkat.Socket")
if li_rc < 0 then
    destroy loo_Socket
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_MaxWaitMs = 5000
li_Success = loo_Socket.Connect("content.dropboxapi.com",443,1,li_MaxWaitMs)
if li_Success <> 1 then
    Write-Debug loo_Socket.LastErrorText
    Write-Debug "Connect Fail Reason: " + string(loo_Socket.ConnectFailReason)
    destroy loo_Socket
    return
end if

// Set the download bandwidth throttle rate to 50000 bytes per second.
loo_Socket.BandwidthThrottleDown = 50000

loo_Rest = create oleobject
// Use "Chilkat_9_5_0.Rest" for versions of Chilkat < 10.0.0
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

// Tell the REST object to use the connected socket.
loo_Rest.UseConnection(loo_Socket,1)

// The remainder of this example is identical to the example at:
// Dropbox Download File.

// Add request headers.
loo_Rest.AddHeader("Authorization","Bearer DROPBOX_ACCESS_TOKEN")

// The download "parameters" are contained in JSON passed in an HTTP request header.
// This is the JSON indicating the file to be downloaded:
// { 
//    "path": "/Homework/lit/hamlet.xml",
// }

loo_Json = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.AppendString("path","/Homework/lit/hamlet.xml")
loo_Rest.AddHeader("Dropbox-API-Arg",loo_Json.Emit())

// Setup a file stream for the download
loo_FileStream = create oleobject
// Use "Chilkat_9_5_0.Stream" for versions of Chilkat < 10.0.0
li_rc = loo_FileStream.ConnectToNewObject("Chilkat.Stream")

loo_FileStream.SinkFile = "qa_output/hamletFromDropbox.xml"

// Indicate that the call to FullRequestNoBody should send the response body
// to fileStream if the response status code is 200.
// If a non-success response status code is received, then nothing
// is streamed to the output file and the error response is returned by FullRequestNoBody.
li_ExpectedStatus = 200
loo_Rest.SetResponseBodyStream(li_ExpectedStatus,1,loo_FileStream)

ls_ResponseStr = loo_Rest.FullRequestNoBody("POST","/2/files/download")
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Socket
    destroy loo_Rest
    destroy loo_Json
    destroy loo_FileStream
    return
end if

// When successful, Dropbox responds with a 200 response code.
if loo_Rest.ResponseStatusCode <> 200 then
    // Examine the request/response to see what happened.
    Write-Debug "response status code = " + string(loo_Rest.ResponseStatusCode)
    Write-Debug "response status text = " + loo_Rest.ResponseStatusText
    Write-Debug "response header: " + loo_Rest.ResponseHeader
    Write-Debug "response body (if any): " + ls_ResponseStr
    Write-Debug "---"
    Write-Debug "LastRequestStartLine: " + loo_Rest.LastRequestStartLine
    Write-Debug "LastRequestHeader: " + loo_Rest.LastRequestHeader
    destroy loo_Socket
    destroy loo_Rest
    destroy loo_Json
    destroy loo_FileStream
    return
end if

// Information about the downloaded file is also available as JSON in a response header.
// The "dropbox-api-result" response header contains the information.  For example:
ls_ApiResult = loo_Rest.ResponseHdrByName("dropbox-api-result")
Write-Debug ls_ApiResult

// In this case, the pretty-formatted dropbox-api-result JSON looks like this:
// { 
//   "name": "hamlet.xml",
//   "path_lower": "/homework/lit/hamlet.xml",
//   "path_display": "/Homework/lit/hamlet.xml",
//   "id": "id:74FkdeNuyKAAAAAAAAAAAQ",
//   "client_modified": "2016-06-02T23:19:00Z",
//   "server_modified": "2016-06-02T23:19:00Z",
//   "rev": "9482db15f",
//   "size": 279658
// }

// Load the JSON, pretty-print it, and demonstrate how to get some values...
loo_JsonResult = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JsonResult.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonResult.EmitCompact = 0
loo_JsonResult.Load(ls_ApiResult)
// Show the JSON pretty-printed...
Write-Debug loo_JsonResult.Emit()

// Sample code to get data from the JSON response:
li_Size = loo_JsonResult.IntOf("size")
Write-Debug "size = " + string(li_Size)

ls_Rev = loo_JsonResult.StringOf("rev")
Write-Debug "rev = " + ls_Rev

ls_ClientModified = loo_JsonResult.StringOf("client_modified")
loo_Ckdt = create oleobject
// Use "Chilkat_9_5_0.CkDateTime" for versions of Chilkat < 10.0.0
li_rc = loo_Ckdt.ConnectToNewObject("Chilkat.CkDateTime")

loo_Ckdt.SetFromTimestamp(ls_ClientModified)
li_BLocalTime = 1
loo_Dt = loo_Ckdt.GetDtObj(li_BLocalTime)
Write-Debug string(loo_Dt.Day) + "/" + string(loo_Dt.Month) + "/" + string(loo_Dt.Year) + " " + string(loo_Dt.Hour) + ":" + string(loo_Dt.Minute)
destroy loo_Dt


destroy loo_Socket
destroy loo_Rest
destroy loo_Json
destroy loo_FileStream
destroy loo_JsonResult
destroy loo_Ckdt

 

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