DataFlex
DataFlex
Example: Http.DownloadAppend method
Demonstrates the DownloadAppend method.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
String sTargetPath
Handle hoHttp
Integer iStatusCode
Handle hoSb
Handle hoFac
String sTemp1
Move False To iSuccess
Move False To iSuccess
Move "c:/temp/qa_output/download.txt" To sTargetPath
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComKeepResponseBody Of hoHttp To True
// Assume the target file in the local filesystem does not yet exist.
Get ComDownloadAppend Of hoHttp "https://chilkatsoft.com/testData/helloWorld.txt" sTargetPath To iSuccess
Get ComLastStatus Of hoHttp To iStatusCode
If (iStatusCode = 0) Begin
// Unable to either send the request or get the response.
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
// Should be 200.
Showln "Response status code: " iStatusCode
// Examine the contents of the file.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
If (Not(IsComObjectCreated(hoSb))) Begin
Send CreateComObject of hoSb
End
Get ComLoadFile Of hoSb sTargetPath "utf-8" To iSuccess
Get ComGetAsString Of hoSb To sTemp1
Showln sTemp1
// Output:
// Response status code: 200
// Hello World!
// Download another text file and append to the target file.
Get ComDownloadAppend Of hoHttp "https://chilkatsoft.com/testData/this_is_a_test.txt" sTargetPath To iSuccess
Get ComLastStatus Of hoHttp To iStatusCode
If (iStatusCode = 0) Begin
// Unable to either send the request or get the response.
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
// Should be 200.
Showln "Response status code: " iStatusCode
// Examine the contents of the file.
Get ComLoadFile Of hoSb sTargetPath "utf-8" To iSuccess
Get ComGetAsString Of hoSb To sTemp1
Showln sTemp1
// Output:
// Response status code: 200
// Hello World!This is a Test.
// Delete the local target file.
Get Create (RefClass(cComCkFileAccess)) To hoFac
If (Not(IsComObjectCreated(hoFac))) Begin
Send CreateComObject of hoFac
End
Get ComFileDelete Of hoFac sTargetPath To iSuccess
// Try to download a file that does not exist:
Get ComDownloadAppend Of hoHttp "https://chilkatsoft.com/testData/does_not_exist.txt" sTargetPath To iSuccess
Get ComLastStatus Of hoHttp To iStatusCode
If (iStatusCode = 0) Begin
// Unable to either send the request or get the response.
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
End
Else Begin
// We got a response, and we already know it wasn't a 200 success response.
// It should be a 404 not found.
Showln "Response status code: " iStatusCode
// Examine the response body.
Showln "Response body:"
Get ComLastResponseBody Of hoHttp To sTemp1
Showln sTemp1
End
End_Procedure