Sample code for 30+ languages & platforms
B4X

Example: Http.DownloadAppend method

Demonstrates the DownloadAppend method.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

Dim targetPath As String = "c:/temp/qa_output/download.txt"

Dim http As ChilkatHttp
http.Initialize("http")
http.KeepResponseBody = True

'  Assume the target file in the local filesystem does not yet exist.
success = http.DownloadAppend("https://chilkatsoft.com/testData/helloWorld.txt", targetPath)
Dim statusCode As Int = http.LastStatus
If statusCode = 0 Then
    '  Unable to either send the request or get the response.
    Log(http.LastErrorText)
    Return
End If


'  Should be 200.
Log("Response status code: " & statusCode)

'  Examine the contents of the file.
Dim sb As ChilkatStringBuilder
sb.Initialize
sb.LoadFile(targetPath, "utf-8")
Log(sb.GetAsString)

'  Output: 
'  Response status code: 200
'  Hello World!

'  Download another text file and append to the target file.
success = http.DownloadAppend("https://chilkatsoft.com/testData/this_is_a_test.txt", targetPath)
statusCode = http.LastStatus
If statusCode = 0 Then
    '  Unable to either send the request or get the response.
    Log(http.LastErrorText)
    Return
End If


'  Should be 200.
Log("Response status code: " & statusCode)

'  Examine the contents of the file.
sb.LoadFile(targetPath, "utf-8")
Log(sb.GetAsString)

'  Output:
'  Response status code: 200
'  Hello World!This is a Test.

'  Delete the local target file.
Dim fac As ChilkatFileAccess
fac.Initialize
fac.FileDelete(targetPath)

'  Try to download a file that does not exist:
success = http.DownloadAppend("https://chilkatsoft.com/testData/does_not_exist.txt", targetPath)
statusCode = http.LastStatus
If statusCode = 0 Then
    '  Unable to either send the request or get the response.
    Log(http.LastErrorText)
Else
    '  We got a response, and we already know it wasn't a 200 success response.
    '  It should be a 404 not found.
    Log("Response status code: " & statusCode)
    '  Examine the response body.
    Log("Response body:")
    Log(http.LastResponseBody)
End If