curl下载地址
当前最新版本为7.61.0
将下载的curl-7.61.0.zip解压,得到curl-7.61.0
在目录curl-7.61.0\winbuild中打开cmd
找到vcvars32.bat的位置(它在VS的安装目录中),在刚刚打开的cmd中运行此bat(目的是设置临时的环境变量,使当前的cmd可以运行nmake)
在cmd中运行nmake /f Makefile.vc mode=dll VC=<VC版本>,例如nmake /f Makefile.vc mode=dll VC=14
构建结束后,curl-7.61.0\builds\libcurl-vc15-x86-release-dll-ipv6-sspi-winssl中的lib\libcurl.lib、bin\libcurl.dll和include\curl\*.h便是我们所需要的库和头文件
GET示例
#include#include #pragma comment(lib, "libcurl.lib")size_t writeFunction(void *data, size_t sizeOfEachObject, size_t numberOfObjects);int main(){ curl_global_init(CURL_GLOBAL_ALL); CURL *curl = curl_easy_init(); if (curl) { const char *url = "https://api.eyekey.com/face/Check/checking?app_id=f89ae61fd63d4a63842277e9144a6bd2&app_key=af1cd33549c54b27ae24aeb041865da2&url=http%3A%2F%2Fpicview01.baomihua.com%2Fphotos%2F20120713%2Fm_14_634778197959062500_40614445.jpg"; curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction); CURLcode curlCode = curl_easy_perform(curl); if (CURLE_OK != curlCode) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(curlCode)); curl_easy_cleanup(curl); } else printf("Something went wrong\n"); curl_global_cleanup(); system("pause"); return 0;}size_t writeFunction(void *data, size_t sizeOfEachObject, size_t numberOfObjects){ char *dataPtr = (char *)data; dataPtr[numberOfObjects] = '\0'; printf("%s", dataPtr); return sizeOfEachObject * numberOfObjects;}
POST示例
#include#include #pragma comment(lib, "libcurl.lib")int main(){ curl_global_init(CURL_GLOBAL_ALL); CURL *curl = curl_easy_init(); if (curl) { const char *url = "https://api.eyekey.com/face/Check/checking"; curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "app_id=f89ae61fd63d4a63842277e9144a6bd2&app_key=af1cd33549c54b27ae24aeb041865da2&url=https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=18bb0cc2dd1373f0f53f68999c342cc6/caef76094b36acafe725024570d98d1000e99c46.jpg"); CURLcode curlCode = curl_easy_perform(curl); if (CURLE_OK != curlCode) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(curlCode)); curl_easy_cleanup(curl); } curl_global_cleanup(); system("pause"); return 0;}
参考链接