Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) eBay -- Download Data using FileTransferServiceDemonstrates how to download a data file using the eBay File Transfer API. Note: This example requires Chilkat v9.5.0.67 or later.
#include <C_CkHttp.h> #include <C_CkHttpRequest.h> #include <C_CkXml.h> #include <C_CkHttpResponse.h> #include <C_CkBinData.h> #include <C_CkMime.h> #include <C_CkStringBuilder.h> #include <C_CkGzip.h> #include <C_CkZip.h> #include <C_CkZipEntry.h> void ChilkatSample(void) { const char *accessToken; HCkHttp http; HCkHttpRequest req; HCkXml xml; HCkHttpResponse resp; int statusCode; HCkBinData responseBody; HCkMime mime; BOOL success; HCkMime part0; const char *downloadResponseXml; HCkXml xmlResp; HCkMime part1; HCkBinData zipData; HCkStringBuilder sbContentType; HCkXml xmlFromZip; HCkGzip gzip; HCkZip zip; HCkZipEntry entry; // 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 = CkHttp_SynchronousRequest(http,"storage.sandbox.ebay.com",443,TRUE,req); if (CkHttp_getLastMethodSuccess(http) != TRUE) { printf("%s\n",CkHttp_lastErrorText(http)); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkXml_Dispose(xml); return; } statusCode = CkHttpResponse_getStatusCode(resp); printf("Response status code = %d\n",statusCode); responseBody = CkBinData_Create(); CkHttpResponse_GetBodyBd(resp,responseBody); CkHttpResponse_Dispose(resp); // 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) { printf("Failed.\n"); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkXml_Dispose(xml); CkBinData_Dispose(responseBody); return; } // 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 != TRUE) { printf("%s\n",CkMime_lastErrorText(mime)); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkXml_Dispose(xml); CkBinData_Dispose(responseBody); CkMime_Dispose(mime); return; } // 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) { printf("Expected the MIME to have 2 parts.\n"); printf("NumParts = %d\n",CkMime_getNumParts(mime)); printf("Failed.\n"); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkXml_Dispose(xml); CkBinData_Dispose(responseBody); CkMime_Dispose(mime); return; } // Get the XML from the 1st MIME sub-part. part0 = CkMime_GetPart(mime,0); downloadResponseXml = CkMime_getBodyDecoded(part0); xmlResp = CkXml_Create(); CkXml_LoadXml(xmlResp,downloadResponseXml); printf("Download Response XML:\n"); printf("%s\n",CkXml_getXml(xmlResp)); CkMime_Dispose(part0); printf("----\n"); // Now get the zip from the second part (index=1), unzip, and examine.. part1 = CkMime_GetPart(mime,1); zipData = CkBinData_Create(); // This example requires Chilkat v9.5.0.67 or later. // The GetBodyBd method was added in v9.5.0.67. CkMime_GetBodyBd(part1,zipData); // Check to see if we have a zip or gzip. sbContentType = CkStringBuilder_Create(); CkStringBuilder_Append(sbContentType,CkMime_contentType(part1)); CkMime_Dispose(part1); xmlFromZip = CkXml_Create(); if (CkStringBuilder_Contains(sbContentType,"gzip",FALSE) == TRUE) { // 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 != TRUE) { printf("%s\n",CkGzip_lastErrorText(gzip)); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkXml_Dispose(xml); CkBinData_Dispose(responseBody); CkMime_Dispose(mime); CkXml_Dispose(xmlResp); CkBinData_Dispose(zipData); CkStringBuilder_Dispose(sbContentType); CkXml_Dispose(xmlFromZip); CkGzip_Dispose(gzip); return; } CkXml_LoadXml(xmlFromZip,CkBinData_getString(zipData,"utf-8")); } else { // This is a zip archive. // Load the body into a Zip object. zip = CkZip_Create(); success = CkZip_OpenBd(zip,zipData); if (success != TRUE) { printf("%s\n",CkZip_lastErrorText(zip)); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkXml_Dispose(xml); CkBinData_Dispose(responseBody); CkMime_Dispose(mime); CkXml_Dispose(xmlResp); CkBinData_Dispose(zipData); CkStringBuilder_Dispose(sbContentType); CkXml_Dispose(xmlFromZip); CkGzip_Dispose(gzip); CkZip_Dispose(zip); return; } // 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) { printf("Expected the .zip to have 1 entry.\n"); printf("NumEntries = %d\n",CkZip_getNumEntries(zip)); printf("Failed.\n"); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkXml_Dispose(xml); CkBinData_Dispose(responseBody); CkMime_Dispose(mime); CkXml_Dispose(xmlResp); CkBinData_Dispose(zipData); CkStringBuilder_Dispose(sbContentType); CkXml_Dispose(xmlFromZip); CkGzip_Dispose(gzip); CkZip_Dispose(zip); return; } entry = CkZip_GetEntryByIndex(zip,0); CkXml_LoadXml(xmlFromZip,CkZipEntry_unzipToString(entry,0,"utf-8")); CkZipEntry_Dispose(entry); } printf("XML contained in the zip:\n"); printf("%s\n",CkXml_getXml(xmlFromZip)); printf("----\n"); printf("Success.\n"); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkXml_Dispose(xml); CkBinData_Dispose(responseBody); CkMime_Dispose(mime); CkXml_Dispose(xmlResp); CkBinData_Dispose(zipData); CkStringBuilder_Dispose(sbContentType); CkXml_Dispose(xmlFromZip); CkGzip_Dispose(gzip); CkZip_Dispose(zip); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.