1.下载
官网:
找到Windows 64-bit,选择第一项下载
2.解压到指定路径,我这里解压到D:\Program Files下面
3.创建c++控制台应用程序,右键点击属性
再点击VC++目录 -> 包含目录,添加路径“D:\Program Files\curl-8.9.1_1-win64-mingw\include;$(IncludePath);”
再点击VC++目录 -> 库目录,添加路径“D:\Program Files\curl-8.9.1_1-win64-mingw\lib;”
再点击链接器 -> 输入 -> 附加依赖项,添加“libcurl.dll.a”和“libcurl.a”
4.打开路径“D:\Program Files\curl-8.9.1_1-win64-mingw\bin”,找到“libcurl-x64.dll”文件,并复制到项目根目录下的“x64/Debug/”下
5.添加头文件和curl的请求代码,如下,点击运行返回请求结果
#include <iostream> #include <curl/curl.h> char* strToChar(std::string strSend) { char* ConvertData; const int len2 = strSend.length(); ConvertData = new char[len2 + 1]; strcpy(ConvertData, strSend.c_str()); return ConvertData; } int main() { std::string _serverUrl = ""; _serverUrl.append("https://www.baidu.com"); CURL* curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, strToChar(_serverUrl)); /* cache the CA cert bundle in memory for a week */ curl_easy_setopt(curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); /* Perform the request, res gets the return code */ res = curl_easy_perform(curl); /* Check for errors */ if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } curl_global_cleanup(); }