How to Screenshot a Website in C/C++
If you’ve ever been the victim of a phishing attack, you know how dangerous unknown links can be; even URLs that look legitimate at first glance may contain an inconsistency that was strategically placed by a cyber attacker. One way you can avoid these potential threats is to take a screenshot of a URL prior to clicking it — this will allow you to view and analyze the content without exposing your device to potential damage. In this quick tutorial, we will show you how to use an API in C/C++ to generate a PNG screenshot of the URL in question.
We will begin by installing libcurl in your C/C++ project:
libcurl/7.75.0
After this, we can call the function with the following code:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/convert/web/url/to/screenshot");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "Url=%3Cstring%3E&ExtraLoadingWait=%3Cinteger%3E&ScreenshotWidth=%3Cinteger%3E&ScreenshotHeight=%3Cinteger%3E";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
And just like that, your full-page image of the website will be available! To retrieve your API key, visit the Cloudmersive website and register for a free account; this will provide you with 800 calls/month across our library of APIs.