Lianja
Lianja
Example: Http.DownloadAppend method
Demonstrates the DownloadAppend method.Chilkat Lianja Downloads
llSuccess = .F.
llSuccess = .F.
lcTargetPath = "c:/temp/qa_output/download.txt"
loHttp = createobject("CkHttp")
loHttp.KeepResponseBody = .T.
// Assume the target file in the local filesystem does not yet exist.
llSuccess = loHttp.DownloadAppend("https://chilkatsoft.com/testData/helloWorld.txt",lcTargetPath)
lnStatusCode = loHttp.LastStatus
if (lnStatusCode = 0) then
// Unable to either send the request or get the response.
? loHttp.LastErrorText
release loHttp
return
endif
// Should be 200.
? "Response status code: " + str(lnStatusCode)
// Examine the contents of the file.
loSb = createobject("CkStringBuilder")
loSb.LoadFile(lcTargetPath,"utf-8")
? loSb.GetAsString()
// Output:
// Response status code: 200
// Hello World!
// Download another text file and append to the target file.
llSuccess = loHttp.DownloadAppend("https://chilkatsoft.com/testData/this_is_a_test.txt",lcTargetPath)
lnStatusCode = loHttp.LastStatus
if (lnStatusCode = 0) then
// Unable to either send the request or get the response.
? loHttp.LastErrorText
release loHttp
release loSb
return
endif
// Should be 200.
? "Response status code: " + str(lnStatusCode)
// Examine the contents of the file.
loSb.LoadFile(lcTargetPath,"utf-8")
? loSb.GetAsString()
// Output:
// Response status code: 200
// Hello World!This is a Test.
// Delete the local target file.
loFac = createobject("CkFileAccess")
loFac.FileDelete(lcTargetPath)
// Try to download a file that does not exist:
llSuccess = loHttp.DownloadAppend("https://chilkatsoft.com/testData/does_not_exist.txt",lcTargetPath)
lnStatusCode = loHttp.LastStatus
if (lnStatusCode = 0) then
// Unable to either send the request or get the response.
? loHttp.LastErrorText
else
// We got a response, and we already know it wasn't a 200 success response.
// It should be a 404 not found.
? "Response status code: " + str(lnStatusCode)
// Examine the response body.
? "Response body:"
? loHttp.LastResponseBody
endif
release loHttp
release loSb
release loFac