#include #include #include #include const char * URL = "http://users.pandora.be/tfautre/dennis_richie_ken_thompson_pdp11.jpeg"; const char * Filename = "image.jpg"; #define INCORRECT // comment out this line to use the correct behavior #if defined INCORRECT ////////////////////////////////////////////////////////////////////// // This version saves the file in text mode ////////////////////////////////////////////////////////////////////// int main(int argc, char * argv[]) { xmlNanoHTTPInit(); if (xmlNanoHTTPFetch(URL, Filename, 0) == -1) std::cerr << "Error: xmlNanoHTTPFetch() == -1" << std::endl; xmlNanoHTTPCleanup(); } #else ////////////////////////////////////////////////////////////////////// // This version saves the file in binary mode ////////////////////////////////////////////////////////////////////// int main(int argc, char * argv[]) { // Initialize xmlNanoHTTPInit(); // Open HTTP connection char * pContentType = 0; void * Ctxt = 0; Ctxt = xmlNanoHTTPOpen(URL, &pContentType); if (Ctxt == 0) std::cerr << "ERROR: xmlNanoHTTPOpen() == 0" << std::endl; if (xmlNanoHTTPReturnCode(Ctxt) != 200) std::cerr << "ERROR: HTTP Code != OK" << std::endl; // Open output file std::ofstream File(Filename, std::ios::binary); if (! File) std::cerr << "ERROR: couldn't open '" << Filename << "' for output" << std::endl; // Write to file const int Size = 2048; char Buffer[Size]; int Count; while ((Count = xmlNanoHTTPRead(Ctxt, Buffer, Size)) > 0) File.write(Buffer, Count); if (Count == 0) std::cerr << "STATUS: Connection closed" << std::endl; if (Count == -1) std::cerr << "ERROR: xmlNanoHTTPRead() == -1" << std::endl; if (xmlNanoHTTPReturnCode(Ctxt) != 200) std::cerr << "ERROR: HTTP Code != OK" << std::endl; // Free ressources xmlFree(pContentType); xmlNanoHTTPClose(Ctxt); xmlNanoHTTPCleanup(); } #endif