Unicode C++
Unicode C++
Example: Http.DownloadAppend method
Demonstrates the DownloadAppend method.Chilkat Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkStringBuilderW.h>
#include <CkFileAccessW.h>
void ChilkatSample(void)
{
bool success = false;
const wchar_t *targetPath = L"c:/temp/qa_output/download.txt";
CkHttpW http;
http.put_KeepResponseBody(true);
// Assume the target file in the local filesystem does not yet exist.
success = http.DownloadAppend(L"https://chilkatsoft.com/testData/helloWorld.txt",targetPath);
int statusCode = http.get_LastStatus();
if (statusCode == 0) {
// Unable to either send the request or get the response.
wprintf(L"%s\n",http.lastErrorText());
return;
}
// Should be 200.
wprintf(L"Response status code: %d\n",statusCode);
// Examine the contents of the file.
CkStringBuilderW sb;
sb.LoadFile(targetPath,L"utf-8");
wprintf(L"%s\n",sb.getAsString());
// Output:
// Response status code: 200
// Hello World!
// Download another text file and append to the target file.
success = http.DownloadAppend(L"https://chilkatsoft.com/testData/this_is_a_test.txt",targetPath);
statusCode = http.get_LastStatus();
if (statusCode == 0) {
// Unable to either send the request or get the response.
wprintf(L"%s\n",http.lastErrorText());
return;
}
// Should be 200.
wprintf(L"Response status code: %d\n",statusCode);
// Examine the contents of the file.
sb.LoadFile(targetPath,L"utf-8");
wprintf(L"%s\n",sb.getAsString());
// Output:
// Response status code: 200
// Hello World!This is a Test.
// Delete the local target file.
CkFileAccessW fac;
fac.FileDelete(targetPath);
// Try to download a file that does not exist:
success = http.DownloadAppend(L"https://chilkatsoft.com/testData/does_not_exist.txt",targetPath);
statusCode = http.get_LastStatus();
if (statusCode == 0) {
// Unable to either send the request or get the response.
wprintf(L"%s\n",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.
wprintf(L"Response status code: %d\n",statusCode);
// Examine the response body.
wprintf(L"Response body:\n");
wprintf(L"%s\n",http.lastResponseBody());
}
}