On windows platform, goto the unpack folder, such as d:/libcurl/curl, find the winbuild diretory. Open the vs command line window and use “nmake makefile.vc” to compile the code, here is the sample to compile the libcurl library to x86 debug static library.
nmake makefile.vc vc=10 mode=static machine=x86 debug=yes
#define CURL_STATICLIB
#include <curl/curl.h>
#include <curl/easy.h>
#include <curl/multi.h>
Easy interafce
curl_easy_init
This function must be the first function to call, and it returns a CURL easy handle that you must use as input to other easy-functions. curl_easy_init contains curl_global_init invoking automatically if you have not yet called it. But this is lethal in multi-threaded cases, since curl_global_init is not thread-safe.
You are strongly advised to not allow this automatic behaviour, by calling curl_global_init yourself properly.
If it returns NULL to curl pointer, something went wrong and you can not use the other funcitons.
curl_easy_cleanup
It is oppsoite of the curl_easy_init function and it should be the last function call for an easy sesstion. Any uses of the handlel after this function has been called and hanve returned, are illegal. This function return None.
CURL * curl = nullptr;
char * esc = nullptr;
CURLcode ret;
ret = curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
esc = curl_easy_escape(curl, "http://lcoalhost", 0);
cout<<esc<<endl;
curl_free((void*)esc);
curl_easy_cleanup(curl);
Using C++ non-static functions for callbacks?
libcurl is a C library, it doesn't know anything about C++ member functions.
You can overcome this "limitation" with a relative ease using a static member function that is passed a pointer to the class:
// f is the pointer to your object.
static YourClass::func(void *buffer, size_t sz, size_t n, void *f)
{
// Call non-static member function.
static_cast<YourClass*>(f)->nonStaticFunction();
}
// This is how you pass pointer to the static function:
curl_easy_setopt(hcurl, CURLOPT_WRITEFUNCTION, YourClass::func);
curl_easy_setopt(hcurl, CURLOPT_WRITEDATA, this);
Get the remote file’s size
curl_easy_setopt(handle, CURLOPT_URL, url);get information
curl_easy_setopt(handle, CURLOPT_PROXY, szProxy); // for proxy
curl_easy_setopt(handle, CURLOPT_NOBODY, 1);
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, szErr);
ret = curl_easy_perform(handle);
double dval;
if (CURLE_OK != curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dval))
{
cerr<<"Failed to get CURLINFO_CONTENT_LENGTH_DOWNLOAD"<<endl;
curl_easy_cleanup(curl);
return;
}
cout<<"CURLINFO_CONTENT_LENGTH_DOWNLOAD :"<<dval<<endl;
Get the remote file’s create time
curl_easy_setopt(handle, CURLOPT_FILETIME, 1L); // for curl_easy_getinfo() for CURLINFO_FILETIMEget information
long lval;
if (CURLE_OK != curl_easy_getinfo(curl, CURLINFO_FILETIME, &lval) || lval == -1)
{
cerr<<"Failed to get CURLINFO_FILETIME"<<endl;
curl_easy_cleanup(curl);
return;
}
time_t t(lval);
tm * ptm = localtime((const time_t*)&t);
cout<<"CURLINFO_FILETIME :"<<asctime(ptm)<<endl;
Show progress for downloading
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);change to use