Delphi DLL
Delphi DLL
Example: Http.DownloadAppend method
Demonstrates the DownloadAppend method.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, FileAccess;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
targetPath: PWideChar;
http: HCkHttp;
statusCode: Integer;
sb: HCkStringBuilder;
fac: HCkFileAccess;
begin
success := False;
success := False;
targetPath := 'c:/temp/qa_output/download.txt';
http := CkHttp_Create();
CkHttp_putKeepResponseBody(http,True);
// Assume the target file in the local filesystem does not yet exist.
success := CkHttp_DownloadAppend(http,'https://chilkatsoft.com/testData/helloWorld.txt',targetPath);
statusCode := CkHttp_getLastStatus(http);
if (statusCode = 0) then
begin
// Unable to either send the request or get the response.
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
// Should be 200.
Memo1.Lines.Add('Response status code: ' + IntToStr(statusCode));
// Examine the contents of the file.
sb := CkStringBuilder_Create();
CkStringBuilder_LoadFile(sb,targetPath,'utf-8');
Memo1.Lines.Add(CkStringBuilder__getAsString(sb));
// Output:
// Response status code: 200
// Hello World!
// Download another text file and append to the target file.
success := CkHttp_DownloadAppend(http,'https://chilkatsoft.com/testData/this_is_a_test.txt',targetPath);
statusCode := CkHttp_getLastStatus(http);
if (statusCode = 0) then
begin
// Unable to either send the request or get the response.
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
// Should be 200.
Memo1.Lines.Add('Response status code: ' + IntToStr(statusCode));
// Examine the contents of the file.
CkStringBuilder_LoadFile(sb,targetPath,'utf-8');
Memo1.Lines.Add(CkStringBuilder__getAsString(sb));
// Output:
// Response status code: 200
// Hello World!This is a Test.
// Delete the local target file.
fac := CkFileAccess_Create();
CkFileAccess_FileDelete(fac,targetPath);
// Try to download a file that does not exist:
success := CkHttp_DownloadAppend(http,'https://chilkatsoft.com/testData/does_not_exist.txt',targetPath);
statusCode := CkHttp_getLastStatus(http);
if (statusCode = 0) then
begin
// Unable to either send the request or get the response.
Memo1.Lines.Add(CkHttp__lastErrorText(http));
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.
Memo1.Lines.Add('Response status code: ' + IntToStr(statusCode));
// Examine the response body.
Memo1.Lines.Add('Response body:');
Memo1.Lines.Add(CkHttp__lastResponseBody(http));
end;
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sb);
CkFileAccess_Dispose(fac);
end;