PowerBuilder
PowerBuilder
REST Receive Response in Chunks
See more REST Examples
Demonstrates how to receive a REST HTTP response in chunks.Note: This example requires Chilkat 10.1.0 or greater.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_Port
integer li_BAutoReconnect
integer li_StatusCode
string ls_OutputFile
oleobject loo_Fac
oleobject loo_Bd
integer li_Status
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
destroy loo_Rest
MessageBox("Error","Connecting to COM object failed")
return
end if
// Connect to the web server
li_BTls = 1
li_Port = 443
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("chilkatsoft.com",li_Port,li_BTls,li_BAutoReconnect)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
return
end if
// Send the request.
// This can be *any* kind of request: POST, GET, PUT, etc. using *any* of the Chilkat REST methods that send requests.
// For this example, we'll just GET a simple XML document that is about 274K in size.
li_Success = loo_Rest.SendReqNoBody("GET","/hamlet.xml")
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
return
end if
// Get the response header.
li_StatusCode = loo_Rest.ReadResponseHeader()
if li_StatusCode < 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
return
end if
Write-Debug "response status code = " + string(li_StatusCode)
ls_OutputFile = "c:/temp/qa_output/hamlet.xml"
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")
li_Success = loo_Fac.OpenForWrite(ls_OutputFile)
if li_StatusCode < 0 then
Write-Debug loo_Fac.LastErrorText
destroy loo_Rest
destroy loo_Fac
return
end if
// Get the response in chunks.
// (Note: There are more efficient ways to simply download a file from a web server, such as by calling Chilkat's Http.Download method.
// The purpose of this method is to show how to receive a response chunk-by-chunk.)
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")
li_Status = 1
do while li_Status = 1
// Read a minimum of 16000 bytes.
// Note: Because of TLS message lengths, or the possibility of the response being either compressed (gzip/deflate) or in the HTTP chunked encoding,
// the amount of data received in each call can be greater than the specified min size.
// Chilkat will return from the call as soon as it has received an amount equal to or more than the specified size,
// except for the very last chunk, which can be less that the min size or even 0 bytes.
// The status will be one of three values:
// -1 = error
// 0 = received the last chunk of the response.
// 1 = received a chunk, and more chunks are coming..
// The received data is *appended* to the contents of the BinData object.
li_Status = loo_Rest.ReadRespChunkBd(16000,loo_Bd)
if li_Status >= 0 then
Write-Debug "Received chunk: " + string(loo_Bd.NumBytes) + " bytes"
loo_Fac.FileWriteBd(loo_Bd,0,0)
loo_Bd.Clear()
end if
loop
loo_Fac.FileClose()
Write-Debug "Success."
destroy loo_Rest
destroy loo_Fac
destroy loo_Bd