ReadData

Description

Reads the response body.

Applies to

HTTPClient objects

Syntax

objectname.ReadData ( data, bufferSize )

Argument

Description

objectname

The name of the HTTPClient object for which you want to read the response body.

data

A blob value into which the function returns data.

bufferSize

A long value specifying the buffer size.


Return value

Integer.

Returns values as follows. If any argument's value is null, the method returns null.

1 -- Success

0 -- Reading data is finished

-1 -- General error

-2 -- Timed out

Example 1

Integer li_rc
Blob lblb_photo, lblb_NextData
HttpClient lnv_HttpClient 

lnv_HttpClient = Create HttpClient

// Not to read data automatically after sending request (default is true)
lnv_HttpClient.AutoReadData = false

// Send request using GET method
li_rc = lnv_HttpClient.SendRequest("GET", "https://demo.appeon.com/PB/webapi_client/employee/102/photo")

// Receive large data
if li_rc = 1 and lnv_HttpClient.GetResponseStatusCode() = 200 then
 do while true
  li_rc = lnv_HttpClient.ReadData(lblb_NextData, 1024*16)
  if li_rc = 0 then exit // Finish receiving data
  if li_rc = -1 then exit // Error occurred
  lblb_photo += lblb_NextData
 loop
end if

Example 2

This example demonstrates how to get large data (over 20 MB):

Long ll_FileNum, ll_rtn, ll_loop, ll_len
Dec{0} ldc_Count, ldc_Length
Blob lb_temp
String ls_url, ls_response, ls_Length

HttpClient lhc_Client

lhc_Client = Create HttpClient

lhc_Client.ClearRequestHeaders()
ls_url  = "https://download.test.com/file001.zip"

// Use HEAD method to get the file size
ll_rtn = lhc_Client.sendrequest( "HEAD", ls_url )
ls_response = lhc_Client.GetResponseHeaders()
ls_Length = lhc_Client.GetResponseHeader( "Content-Length" )
ldc_Length = Dec ( ls_Length )
If ldc_Length <= 0 Then
                MessageBox( "Tips", "File length is zero." )
                Return
End If
ll_len = Long ( ldc_Length / 10000 )
hpb_1.maxposition = 10000  // hpb_1 is a hprogressbar control
hpb_1.position = 0

// Not to read data automatically after sending request (default is true)
lhc_Client.autoreaddata = false
ll_rtn = lhc_Client.sendrequest( "GET", ls_url )
//Receive 16KB data every time
ll_loop = 1024 * 16
//Write data to the file, because the blob variable is not suitable for large data
ll_FileNum = FileOpen("file001.zip", StreamMode!, Write!, LockWrite!, Replace!)
Do While ( ll_rtn = 1 )
                lb_temp = Blob ( "" )
                ll_rtn = lhc_Client.ReadData( lb_temp, ll_loop)
                FileWrite(ll_FileNum, lb_temp)
                ldc_Count  += Len ( lb_temp )
                hpb_1.position = Long( ldc_Count/ll_len)
                yield()
Loop

FileClose(ll_FileNum)
If IsValid ( lhc_Client ) Then Destroy ( lhc_Client )