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
(Unicode 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_CkHttpW.h> #include <C_CkHttpRequestW.h> #include <C_CkXmlW.h> #include <C_CkHttpResponseW.h> #include <C_CkBinDataW.h> #include <C_CkMimeW.h> #include <C_CkStringBuilderW.h> #include <C_CkGzipW.h> #include <C_CkZipW.h> #include <C_CkZipEntryW.h> void ChilkatSample(void) { const wchar_t *accessToken; HCkHttpW http; HCkHttpRequestW req; HCkXmlW xml; HCkHttpResponseW resp; int statusCode; HCkBinDataW responseBody; HCkMimeW mime; BOOL success; HCkMimeW part0; const wchar_t *downloadResponseXml; HCkXmlW xmlResp; HCkMimeW part1; HCkBinDataW zipData; HCkStringBuilderW sbContentType; HCkXmlW xmlFromZip; HCkGzipW gzip; HCkZipW zip; HCkZipEntryW 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 = L"EBAY_ACCESS_TOKEN"; http = CkHttpW_Create(); req = CkHttpRequestW_Create(); CkHttpRequestW_putHttpVerb(req,L"POST"); CkHttpRequestW_putPath(req,L"/FileTransferService"); CkHttpRequestW_putContentType(req,L"application/xml"); // Build the XML body for the request. xml = CkXmlW_Create(); CkXmlW_putTag(xml,L"downloadFileRequest"); CkXmlW_AddAttribute(xml,L"xmlns",L"http://www.ebay.com/marketplace/services"); CkXmlW_UpdateChildContent(xml,L"taskReferenceId",L"50013004806"); CkXmlW_UpdateChildContent(xml,L"fileReferenceId",L"50015579016"); CkHttpRequestW_LoadBodyFromString(req,CkXmlW_getXml(xml),L"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> CkHttpRequestW_AddHeader(req,L"X-EBAY-SOA-OPERATION-NAME",L"downloadFile"); CkHttpRequestW_AddHeader(req,L"X-EBAY-SOA-SECURITY-TOKEN",accessToken); resp = CkHttpW_SynchronousRequest(http,L"storage.sandbox.ebay.com",443,TRUE,req); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkXmlW_Dispose(xml); return; } statusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Response status code = %d\n",statusCode); responseBody = CkBinDataW_Create(); CkHttpResponseW_GetBodyBd(resp,responseBody); CkHttpResponseW_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.) CkBinDataW_WriteFile(responseBody,L"qa_output/response.mime"); if (statusCode != 200) { wprintf(L"Failed.\n"); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkXmlW_Dispose(xml); CkBinDataW_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 = CkMimeW_Create(); success = CkMimeW_LoadMimeBd(mime,responseBody); if (success != TRUE) { wprintf(L"%s\n",CkMimeW_lastErrorText(mime)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkXmlW_Dispose(xml); CkBinDataW_Dispose(responseBody); CkMimeW_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 (CkMimeW_getNumParts(mime) != 2) { wprintf(L"Expected the MIME to have 2 parts.\n"); wprintf(L"NumParts = %d\n",CkMimeW_getNumParts(mime)); wprintf(L"Failed.\n"); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkXmlW_Dispose(xml); CkBinDataW_Dispose(responseBody); CkMimeW_Dispose(mime); return; } // Get the XML from the 1st MIME sub-part. part0 = CkMimeW_GetPart(mime,0); downloadResponseXml = CkMimeW_getBodyDecoded(part0); xmlResp = CkXmlW_Create(); CkXmlW_LoadXml(xmlResp,downloadResponseXml); wprintf(L"Download Response XML:\n"); wprintf(L"%s\n",CkXmlW_getXml(xmlResp)); CkMimeW_Dispose(part0); wprintf(L"----\n"); // Now get the zip from the second part (index=1), unzip, and examine.. part1 = CkMimeW_GetPart(mime,1); zipData = CkBinDataW_Create(); // This example requires Chilkat v9.5.0.67 or later. // The GetBodyBd method was added in v9.5.0.67. CkMimeW_GetBodyBd(part1,zipData); // Check to see if we have a zip or gzip. sbContentType = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbContentType,CkMimeW_contentType(part1)); CkMimeW_Dispose(part1); xmlFromZip = CkXmlW_Create(); if (CkStringBuilderW_Contains(sbContentType,L"gzip",FALSE) == TRUE) { // This is a gzip compressed file. gzip = CkGzipW_Create(); // in-place uncompress the data. // Note: The UncompressBd method was added in Chilkat v9.5.0.67 success = CkGzipW_UncompressBd(gzip,zipData); if (success != TRUE) { wprintf(L"%s\n",CkGzipW_lastErrorText(gzip)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkXmlW_Dispose(xml); CkBinDataW_Dispose(responseBody); CkMimeW_Dispose(mime); CkXmlW_Dispose(xmlResp); CkBinDataW_Dispose(zipData); CkStringBuilderW_Dispose(sbContentType); CkXmlW_Dispose(xmlFromZip); CkGzipW_Dispose(gzip); return; } CkXmlW_LoadXml(xmlFromZip,CkBinDataW_getString(zipData,L"utf-8")); } else { // This is a zip archive. // Load the body into a Zip object. zip = CkZipW_Create(); success = CkZipW_OpenBd(zip,zipData); if (success != TRUE) { wprintf(L"%s\n",CkZipW_lastErrorText(zip)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkXmlW_Dispose(xml); CkBinDataW_Dispose(responseBody); CkMimeW_Dispose(mime); CkXmlW_Dispose(xmlResp); CkBinDataW_Dispose(zipData); CkStringBuilderW_Dispose(sbContentType); CkXmlW_Dispose(xmlFromZip); CkGzipW_Dispose(gzip); CkZipW_Dispose(zip); return; } // Save the .zip to a file (so we can examine it for debugging if something is not as expected) CkBinDataW_WriteFile(zipData,L"qa_output/ebay_data.zip"); // The zip should contain a single XML file. if (CkZipW_getNumEntries(zip) != 1) { wprintf(L"Expected the .zip to have 1 entry.\n"); wprintf(L"NumEntries = %d\n",CkZipW_getNumEntries(zip)); wprintf(L"Failed.\n"); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkXmlW_Dispose(xml); CkBinDataW_Dispose(responseBody); CkMimeW_Dispose(mime); CkXmlW_Dispose(xmlResp); CkBinDataW_Dispose(zipData); CkStringBuilderW_Dispose(sbContentType); CkXmlW_Dispose(xmlFromZip); CkGzipW_Dispose(gzip); CkZipW_Dispose(zip); return; } entry = CkZipW_GetEntryByIndex(zip,0); CkXmlW_LoadXml(xmlFromZip,CkZipEntryW_unzipToString(entry,0,L"utf-8")); CkZipEntryW_Dispose(entry); } wprintf(L"XML contained in the zip:\n"); wprintf(L"%s\n",CkXmlW_getXml(xmlFromZip)); wprintf(L"----\n"); wprintf(L"Success.\n"); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkXmlW_Dispose(xml); CkBinDataW_Dispose(responseBody); CkMimeW_Dispose(mime); CkXmlW_Dispose(xmlResp); CkBinDataW_Dispose(zipData); CkStringBuilderW_Dispose(sbContentType); CkXmlW_Dispose(xmlFromZip); CkGzipW_Dispose(gzip); CkZipW_Dispose(zip); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.