1.安装curllib环境,这里有一篇挺详细的教程和一些介绍https://blog.csdn.net/byxdaz/article/details/81869881
2.加入头文件:
#include "curl/include/win32/curl/curl.h";//我现在在做cocos2dx的一个项目,里面已经有curllib了,所以路径是这个
#pragma comment(lib, "libcurl.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "wldap32.lib")
3.加入读数据的一个回调函数:
static size_t downloadCallback(void* buffer, size_t sz, size_t nmemb, void* writer)
{
string* psResponse = (string*)writer;
size_t size = sz * nmemb;
psResponse->append((char*)buffer, size);
return sz * nmemb;
}
4.读取数据:
CURL* curl = curl_easy_init();
if (nullptr == curl)
{
printf("(curl is NULL!/r/n");
}
string strUrl = "http://localhost:8080/ParkourService/register?mail=" + mail + "&password=" + password;
string strTmpStr;
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strTmpStr);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
string strRsp;
if (res != CURLE_OK)
{
strRsp = "error";
}
else
{
strRsp = strTmpStr;
}
在这里,url自己设计调配,最后的数据就在string的strRsp里面了