Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
eBay -- Download Data using FileTransferService
See more eBay Examples
Demonstrates how to download a data file using the eBay File Transfer API.Chilkat Delphi DLL Downloads
var
success: Boolean;
accessToken: PWideChar;
http: HCkHttp;
req: HCkHttpRequest;
xml: HCkXml;
resp: HCkHttpResponse;
statusCode: Integer;
responseBody: HCkBinData;
mime: HCkMime;
part0: HCkMime;
downloadResponseXml: PWideChar;
xmlResp: HCkXml;
part1: HCkMime;
zipData: HCkBinData;
sbContentType: HCkStringBuilder;
xmlFromZip: HCkXml;
gzip: HCkGzip;
zip: HCkZip;
entry: HCkZipEntry;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Use a previously obtained access token. The token should look something like this:
// "AgAAAA**AQA ..."
accessToken := 'EBAY_ACCESS_TOKEN';
http := CkHttp_Create();
req := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(req,'POST');
CkHttpRequest_putPath(req,'/FileTransferService');
CkHttpRequest_putContentType(req,'application/xml');
// Build the XML body for the request.
xml := CkXml_Create();
CkXml_putTag(xml,'downloadFileRequest');
CkXml_AddAttribute(xml,'xmlns','http://www.ebay.com/marketplace/services');
CkXml_UpdateChildContent(xml,'taskReferenceId','50013004806');
CkXml_UpdateChildContent(xml,'fileReferenceId','50015579016');
CkHttpRequest_LoadBodyFromString(req,CkXml__getXml(xml),'utf-8');
// The XML body looks like this:
// <?xml version="1.0" encoding="UTF-8"?>
// <downloadFileRequest xmlns="http://www.ebay.com/marketplace/services">
// <taskReferenceId>50013004806</taskReferenceId>
// <fileReferenceId>50015579016</fileReferenceId>
// </downloadFileRequest>
CkHttpRequest_AddHeader(req,'X-EBAY-SOA-OPERATION-NAME','downloadFile');
CkHttpRequest_AddHeader(req,'X-EBAY-SOA-SECURITY-TOKEN',accessToken);
resp := CkHttpResponse_Create();
success := CkHttp_HttpSReq(http,'storage.sandbox.ebay.com',443,True,req,resp);
if (success = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
statusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response status code = ' + IntToStr(statusCode));
responseBody := CkBinData_Create();
CkHttpResponse_GetBodyBd(resp,responseBody);
// We can save the response body to a file for examination if we get an unanticipated response.
// (It's binary, so it won't open well in a text editor.)
CkBinData_WriteFile(responseBody,'qa_output/response.mime');
if (statusCode <> 200) then
begin
Memo1.Lines.Add('Failed.');
Exit;
end;
// The response body looks like this:
// --MIMEBoundaryurn_uuid_2B668636C1E17A4D4114925305818684241
// Content-Type: application/xop+xml; charset=utf-8; type="text/xml"
// Content-Transfer-Encoding: binary
// Content-ID: <0.urn:uuid:2B668636C1E17A4D4114925305818684242>
//
// <?xml version='1.0' encoding='UTF-8'?>
// <downloadFileResponse xmlns="http://www.ebay.com/marketplace/services">
// <ack>Success</ack>
// <version>1.1.0</version>
// <timestamp>2017-04-18T15:49:41.868Z</timestamp>
// <fileAttachment>
// <Size>587</Size>
// <Data>
// <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:urn:uuid:A37C3C73E994C267F11492530585522"/>
// </Data>
// </fileAttachment>
// </downloadFileResponse>
// --MIMEBoundaryurn_uuid_2B668636C1E17A4D4114925305818684241
// Content-Type: application/zip
// Content-Transfer-Encoding: binary
// Content-ID: <urn:uuid:A37C3C73E994C267F11492530585522>
//
// <the binary bytes of the zip start here...>
//
// Load the binary response into a MIME object.
mime := CkMime_Create();
success := CkMime_LoadMimeBd(mime,responseBody);
if (success = False) then
begin
Memo1.Lines.Add(CkMime__lastErrorText(mime));
Exit;
end;
// Make sure we have 2 sub-parts. The 1st sub-part is the XML response, the 2nd sub-part
// is the zip containing the data.
// Note: The 2nd sub-part can be a "zip" or "gzip". These are two different file formats.
// A zip is indicated with a Content-Type header equal to "application/zip",
// whereas a gzip is indicated with a Content-Type header equal to "application/x-gzip"
if (CkMime_getNumParts(mime) <> 2) then
begin
Memo1.Lines.Add('Expected the MIME to have 2 parts.');
Memo1.Lines.Add('NumParts = ' + IntToStr(CkMime_getNumParts(mime)));
Memo1.Lines.Add('Failed.');
Exit;
end;
// Get the XML from the 1st MIME sub-part.
part0 := CkMime_Create();
success := CkMime_PartAt(mime,0,part0);
if (success = False) then
begin
Memo1.Lines.Add(CkMime__lastErrorText(mime));
Exit;
end;
downloadResponseXml := CkMime__getBodyDecoded(part0);
xmlResp := CkXml_Create();
CkXml_LoadXml(xmlResp,downloadResponseXml);
Memo1.Lines.Add('Download Response XML:');
Memo1.Lines.Add(CkXml__getXml(xmlResp));
Memo1.Lines.Add('----');
// Now get the zip from the second part (index=1), unzip, and examine..
part1 := CkMime_Create();
success := CkMime_PartAt(mime,1,part1);
if (success = False) then
begin
Memo1.Lines.Add(CkMime__lastErrorText(mime));
Exit;
end;
zipData := CkBinData_Create();
CkMime_GetBodyBd(part1,zipData);
// Check to see if we have a zip or gzip.
sbContentType := CkStringBuilder_Create();
CkStringBuilder_Append(sbContentType,CkMime__contentType(part1));
xmlFromZip := CkXml_Create();
if (CkStringBuilder_Contains(sbContentType,'gzip',False) = True) then
begin
// This is a gzip compressed file.
gzip := CkGzip_Create();
// in-place uncompress the data.
// Note: The UncompressBd method was added in Chilkat v9.5.0.67
success := CkGzip_UncompressBd(gzip,zipData);
if (success = False) then
begin
Memo1.Lines.Add(CkGzip__lastErrorText(gzip));
Exit;
end;
CkXml_LoadXml(xmlFromZip,CkBinData__getString(zipData,'utf-8'));
end
else
begin
// This is a zip archive.
// Load the body into a Zip object.
zip := CkZip_Create();
success := CkZip_OpenBd(zip,zipData);
if (success = False) then
begin
Memo1.Lines.Add(CkZip__lastErrorText(zip));
Exit;
end;
// Save the .zip to a file (so we can examine it for debugging if something is not as expected)
CkBinData_WriteFile(zipData,'qa_output/ebay_data.zip');
// The zip should contain a single XML file.
if (CkZip_getNumEntries(zip) <> 1) then
begin
Memo1.Lines.Add('Expected the .zip to have 1 entry.');
Memo1.Lines.Add('NumEntries = ' + IntToStr(CkZip_getNumEntries(zip)));
Memo1.Lines.Add('Failed.');
Exit;
end;
entry := CkZipEntry_Create();
success := CkZip_EntryAt(zip,0,entry);
if (success = False) then
begin
Memo1.Lines.Add(CkZip__lastErrorText(zip));
Exit;
end;
CkXml_LoadXml(xmlFromZip,CkZipEntry__unzipToString(entry,0,'utf-8'));
end;
Memo1.Lines.Add('XML contained in the zip:');
Memo1.Lines.Add(CkXml__getXml(xmlFromZip));
Memo1.Lines.Add('----');
Memo1.Lines.Add('Success.');
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkXml_Dispose(xml);
CkHttpResponse_Dispose(resp);
CkBinData_Dispose(responseBody);
CkMime_Dispose(mime);
CkMime_Dispose(part0);
CkXml_Dispose(xmlResp);
CkMime_Dispose(part1);
CkBinData_Dispose(zipData);
CkStringBuilder_Dispose(sbContentType);
CkXml_Dispose(xmlFromZip);
CkGzip_Dispose(gzip);
CkZip_Dispose(zip);
CkZipEntry_Dispose(entry);