HTTP Get explained in C++

I use all type of languages to download files, However, recently I have realised I have never done so in C++. Lets take a look at it today:

Ok here is a simple start, is it compiling ? Looks like it is:

[[email protected] cppdownload]# cat download.cpp
#include <iostream>
using namespace std;
int main()
{
return 0;
}
[[email protected] cppdownload]# g++ download.cpp
[[email protected] cppdownload]# ./a.out

Next step .. Download curl-dev libraries:

yum install libcurl-devel.x86_64

Include the header file in your code:

#include <curl/curl.h>

Thanks to a simple example in curl documentation, your code should look like this:

#include <iostream>
#include <curl/curl.h>
using namespace std;
int main()
{
CURL *curl = curl_easy_init();
if(curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "http://jahansyed.co.uk");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
}

Compile the code and give it reference to curl library and run the program:
g++ download.cpp -lcurl
./a.out

Leave a Reply

Your email address will not be published. Required fields are marked *