Unicode C
Unicode C
Example: Http.DownloadAppend method
Demonstrates the DownloadAppend method.Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkFileAccessW.h>
void ChilkatSample(void)
{
BOOL success;
const wchar_t *targetPath;
HCkHttpW http;
int statusCode;
HCkStringBuilderW sb;
HCkFileAccessW fac;
success = FALSE;
targetPath = L"c:/temp/qa_output/download.txt";
http = CkHttpW_Create();
CkHttpW_putKeepResponseBody(http,TRUE);
// Assume the target file in the local filesystem does not yet exist.
success = CkHttpW_DownloadAppend(http,L"https://chilkatsoft.com/testData/helloWorld.txt",targetPath);
statusCode = CkHttpW_getLastStatus(http);
if (statusCode == 0) {
// Unable to either send the request or get the response.
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
return;
}
// Should be 200.
wprintf(L"Response status code: %d\n",statusCode);
// Examine the contents of the file.
sb = CkStringBuilderW_Create();
CkStringBuilderW_LoadFile(sb,targetPath,L"utf-8");
wprintf(L"%s\n",CkStringBuilderW_getAsString(sb));
// Output:
// Response status code: 200
// Hello World!
// Download another text file and append to the target file.
success = CkHttpW_DownloadAppend(http,L"https://chilkatsoft.com/testData/this_is_a_test.txt",targetPath);
statusCode = CkHttpW_getLastStatus(http);
if (statusCode == 0) {
// Unable to either send the request or get the response.
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sb);
return;
}
// Should be 200.
wprintf(L"Response status code: %d\n",statusCode);
// Examine the contents of the file.
CkStringBuilderW_LoadFile(sb,targetPath,L"utf-8");
wprintf(L"%s\n",CkStringBuilderW_getAsString(sb));
// Output:
// Response status code: 200
// Hello World!This is a Test.
// Delete the local target file.
fac = CkFileAccessW_Create();
CkFileAccessW_FileDelete(fac,targetPath);
// Try to download a file that does not exist:
success = CkHttpW_DownloadAppend(http,L"https://chilkatsoft.com/testData/does_not_exist.txt",targetPath);
statusCode = CkHttpW_getLastStatus(http);
if (statusCode == 0) {
// Unable to either send the request or get the response.
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
}
else {
// We got a response, and we already know it wasn't a 200 success response.
// It should be a 404 not found.
wprintf(L"Response status code: %d\n",statusCode);
// Examine the response body.
wprintf(L"Response body:\n");
wprintf(L"%s\n",CkHttpW_lastResponseBody(http));
}
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sb);
CkFileAccessW_Dispose(fac);
}