C++ cURL

I needed a quick way to download files for one of my applications and QHttp/QNetworkRequest just wasn’t cutting it. I could have used WinINEet but that’s just a pain to use and you have to handle every possible error that each API call may produce.

cURL offers a fantastic way to easily download files from a URL. It’s incredibly easy to use; I don’t see why anyone would want to use WinINet.

Here are just a few examples on how to use the cURL API. I will add a guide on how to set it up with VC++ later on.

Example 1: Downloading straight to a file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int http_code = 0;
FILE* fp = 0;
fopen_s(&fp, "index.html", "wb");
if(fp)
{
// Initialize cURL
CURL* curl = curl_easy_init();
// Set the file to write out to
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
// Set the URL to download
curl_easy_setopt(curl, CURLOPT_URL, "www.example.com");
// Download
curl_easy_perform(curl);
// Get the HTTP response code
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
// Clean up
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(fp);
}

Example 2: Downloading to an std::string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
size_t curl_writefunc(void* ptr, size_t size, size_t nmemb, std::string* data)
{
data->append((const char*)ptr, size * nmemb);
return size * nmemb;
}
int http_code = 0;
std::string data;
// Initialize cURL
CURL* curl = curl_easy_init();
// Set the function to call when there is new data
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_writefunc);
// Set the parameter to append the new data to
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
// Set the URL to download
curl_easy_setopt(curl, CURLOPT_URL, "www.example.com");
// Download
curl_easy_perform(curl);
// Get the HTTP response code
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
// Clean up
curl_easy_cleanup(curl);
curl_global_cleanup();

Example 3: Transfer the entire file into a vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
struct xfer
{
// Receive buffer
std::vector<uint8_t> data;
// Constructor
xfer()
{
}
// Destructor
~xfer()
{
}
// Call this before starting a new transfer
void clear()
{
data.clear();
}
};
size_t writefunc(const uint8_t* ptr, size_t size, size_t nmemb, xfer* transfer)
{
// Actual size of the received data
size_t real_size = size * nmemb;
// Copy the new data to the end of the vector
std::copy(ptr, ptr + real_size, std::back_inserter(transfer->data));
return real_size;
}
int http_code = 0;
boost::shared_ptr<xfer> transfer(boost::make_shared<xfer>());
// Initialize cURL
CURL* curl = curl_easy_init();
// Set the URL
curl_easy_setopt(curl, CURLOPT_URL, "www.example.com");
// Set the function to call when there is new data
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
// Set the parameter to append the new data to
curl_easy_setopt(curl, CURLOPT_WRITEDATA, transfer.get());
// Download
curl_easy_perform(curl);
// Get the HTTP response code
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
// Cleanup
curl_easy_cleanup(curl);
curl_global_cleanup();
// Display the contents
printf("%s\n", &transfer->data[0]);

You can also add timeouts to prevent cURL from blocking. The code below will set a 5 second timeout.

1
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);