Sample code for 30+ languages & platforms
PureBasic

Example: Http.DownloadAppend method

Demonstrates the DownloadAppend method.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkFileAccess.pb"

Procedure ChilkatExample()

    success.i = 0

    success = 0

    targetPath.s = "c:/temp/qa_output/download.txt"

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkKeepResponseBody(http, 1)

    ; Assume the target file in the local filesystem does not yet exist.
    success = CkHttp::ckDownloadAppend(http,"https://chilkatsoft.com/testData/helloWorld.txt",targetPath)
    statusCode.i = CkHttp::ckLastStatus(http)
    If statusCode = 0
        ; Unable to either send the request or get the response.
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        ProcedureReturn
    EndIf

    ; Should be 200.
    Debug "Response status code: " + Str(statusCode)

    ; Examine the contents of the file.
    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckLoadFile(sb,targetPath,"utf-8")
    Debug CkStringBuilder::ckGetAsString(sb)

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

    ; Download another text file and append to the target file.
    success = CkHttp::ckDownloadAppend(http,"https://chilkatsoft.com/testData/this_is_a_test.txt",targetPath)
    statusCode = CkHttp::ckLastStatus(http)
    If statusCode = 0
        ; Unable to either send the request or get the response.
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sb)
        ProcedureReturn
    EndIf

    ; Should be 200.
    Debug "Response status code: " + Str(statusCode)

    ; Examine the contents of the file.
    CkStringBuilder::ckLoadFile(sb,targetPath,"utf-8")
    Debug CkStringBuilder::ckGetAsString(sb)

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

    ; Delete the local target file.
    fac.i = CkFileAccess::ckCreate()
    If fac.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkFileAccess::ckFileDelete(fac,targetPath)

    ; Try to download a file that does not exist:
    success = CkHttp::ckDownloadAppend(http,"https://chilkatsoft.com/testData/does_not_exist.txt",targetPath)
    statusCode = CkHttp::ckLastStatus(http)
    If statusCode = 0
        ; Unable to either send the request or get the response.
        Debug CkHttp::ckLastErrorText(http)
    Else
        ; We got a response, and we already know it wasn't a 200 success response.
        ; It should be a 404 not found.
        Debug "Response status code: " + Str(statusCode)
        ; Examine the response body.
        Debug "Response body:"
        Debug CkHttp::ckLastResponseBody(http)
    EndIf



    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sb)
    CkFileAccess::ckDispose(fac)


    ProcedureReturn
EndProcedure