Using libcurl in VC++

OVERVIEW

In this blog post, we take a look at how to setup libcurl for use within Visual Studio. This has been tested in Visual Studio 2022. A pre-requisite is that vcpkg must already be installed . Instructions to setup vcpkg can be found in this blog post. http://truelogic.org/wordpress/2022/07/28/setting-up-wxwidgets-environment-for-visual-studio/ You only need to follow the instructions in the section INSTALL VCPKG. Once vcpkg has been setup, we can continue below.

INSTRUCTIONS

  • From the command prompt. go to the folder where vcpkg is installed. eg. cd c:\vcpkg
  • Type vcpkg install curl:x64-windows and press Enter. This will install libcurl for x64 applications.
  • If you want to install the 32-bit version of libcurl then use the command vcpkg install curl

TEST APPLICATION

Now we can create a small test application to see if libcurl is working. Note the paths for the include and lib files to be used within your application. The curl library would be present within the vcpkg folder. eg.c:\vcpkg\packages. If you installed 32 bit curl then the path would be \vcpkg\packages\curl_x86-windows. For 64 bit curl it would be \vcpkg\packages\curl_x64-windows

  • Create a C++ Console application
  • Go to the Project Properties window. Within that go to VC++ Directories. Click on Include Directories and add the path to the includes folder for libcurl. eg. \vcpkg\packages\curl_x64-windows\include
  • Do the same for Library Directories and add the path to the library folder for libcurl eg. \vcpkg\packages\curl_x64-windows\lib
  • If you have installed 64 bit libcurl then make sure your build is set to x64 otherwise it should be x86.

In the main cpp file of your console application, put in the following code:

#define CURL_STATICLIB
#include <curl\curl.h>
#include <iostream>
using namespace std;

int main() {
	CURL* curl;

	curl = curl_easy_init();
	cout << "Curl initialized" << endl;
	if (curl) {
		curl_easy_setopt(curl, CURLOPT_URL, "https://www.redcross.org/");
		CURLcode res = curl_easy_perform(curl);
		cout << "code " << res << endl;
	} 
	curl_easy_cleanup(curl);
	cout << "Curl closed down" << endl;

}

When you run the code, it should dump the html output from the test url as shown below:

Here is another sample code to do an HTTP POST. Note that in the case of an HTTP Post, we need a callback function which will consolidate all the data returned by the server after the request has been made.

#define CURL_STATICLIB
#include <curl\curl.h>
#include <iostream>
using namespace std;

std::string readBuffer;

size_t postCallback(void* ptr, size_t size, size_t nmemb, std::string* s)
{
	readBuffer.append(static_cast<char*>(ptr), size * nmemb);
	return size * nmemb;
}

void doPost() {
	CURL* curl;

	curl = curl_easy_init();
	cout << "Curl initialized" << endl;
	if (curl) {
		const char* params = "userEmail=regina@dummysite.com&password=35388";
		curl_easy_setopt(curl, CURLOPT_URL, "https://dummysite.com/login");
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, postCallback);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
		curl_easy_setopt(curl, CURLOPT_POST, 1);
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params);
		curl_easy_setopt(curl, CURLOPT_USERAGENT, "Microsoft Edge 3.45");
		curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(params));
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
		CURLcode res = curl_easy_perform(curl);
		cout << "code " << res << endl;
	}
	curl_easy_cleanup(curl);
	cout << "Curl closed down" << endl;

	cout << readBuffer << endl;
}

int main() {
	doPost();

}


Be the first to comment

Leave a Reply

Your email address will not be published.


*