C++ 'GET' request or how do you download files to work with in C++? C++ 'GET' request or how do you download files to work with in C++? curl curl

C++ 'GET' request or how do you download files to work with in C++?


You should be able to bend this to your will.

Now that I have kinda answered your question. Why C++? Nothing against the language, but pick the best language for the job. Perl, PHP, and Python(and I am sure more) all have great documentation and support on this kind of operation.

In perl(the one I am familiar with) it's just about 3-5 lines of code.


Here is the code snippet previously available in (from WayBackMachine):

/* * This is a very simple example of how to use libcurl from within  * a C++  program. The basic idea is that you want to retrieve the  * contents of a web page as a string. Obviously, you can replace  * the buffer object with anything you want and adjust elsewhere  * accordingly. *  * Hope you find it useful.. *  * Todd Papaioannou */#include <string>#include <iostream>#include "curl/curl.h"using namespace std;// Write any errors in herestatic char errorBuffer[CURL_ERROR_SIZE];// Write all expected data in herestatic string buffer;// This is the writer call back function used by curlstatic int writer(char *data, size_t size, size_t nmemb,                  std::string *buffer){  // What we will return  int result = 0;  // Is there anything in the buffer?  if (buffer != NULL)  {    // Append the data to the buffer    buffer->append(data, size * nmemb);    // How much did we write?    result = size * nmemb;  }  return result;}// You know what this does..void usage(){  cout < < "curltest: \n" << endl;  cout << "  Usage:  curltest url\n" << endl;} /* * The old favorite */int main(int argc, char* argv[]){  if (argc > 1)   {    string url(argv[1]);    cout < < "Retrieving " << url << endl;    // Our curl objects    CURL *curl;    CURLcode result;    // Create our curl handle    curl = curl_easy_init();    if (curl)    {      // Now set up all of the curl options      curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);      curl_easy_setopt(curl, CURLOPT_URL, argv[1]);      curl_easy_setopt(curl, CURLOPT_HEADER, 0);      curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);      curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);      // Attempt to retrieve the remote page      result = curl_easy_perform(curl);      // Always cleanup      curl_easy_cleanup(curl);      // Did we succeed?      if (result == CURLE_OK)      {        cout << buffer << "\n";        exit(0);      }      else      {        cout << "Error: [" << result << "] - " << errorBuffer;        exit(-1);      }    }  }}


Since you're on the Win32 platform, there is one built in libary you can use to implement a GET request in a relatively straightforward way: WinInet, which is part of the Win32 SDK. The basic reference for WinInet can be found on MSDN.

Be warned there will be some rough sledding ahead if you're not familiar with the Win32 API. There is a fairly useful block of example code here

You will get linker errors if you fail to add the appropriate library reference to your project. It sounds like you've learned some lessons there already so I'll keep it breif, but be assured that you will find references to both the libraries and header file references you will need in the Win32 documentation (you just have to learn where on the page to look for it).