通过libcurl向服务端发送数据的2种方式:get/post

1.get方式发送数据

    url格式:服务器url地址+ 接口url地址+"?"+发送的数据(特定格式),如:

    http://172.16.3.123:8080/temp/getScreenImages?userDes=4E&userKey=D7B7BA3F24A84D738D8B7A15CC30201A&dataLen=0&data=

  参考代码如下:get方式下载文件并传递数据

   static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if(out && !out->stream) {
out->stream=fopen(out->filename, "wb");//打开文件进行写入
if(!out->stream)
return -1;
}
return fwrite(buffer, size, nmemb, out->stream);
}

    bool cathttpclient::get(IN const std::string &stshortrurl, IN const std::string &strshortdata, 
IN const std::string &strfilepath)
{
bool bret = false;
if (stshortrurl.empty() || strfilepath.empty())
{
LOG_ALL_ERROR(QString("failed download param is empty."));
return bret;
}


std::string strpostdata("");
GetPacketInfo(strshortdata, strpostdata);

//strurl like "http://172.16.3.123:8080/temp/getScreenImages?userDes=4E&userKey=D7B7BA3F24A84D738D8B7A15CC30201A&dataLen=0&data=";
std::string strurl(m_strServerUrl + stshortrurl+"?"+strpostdata);

CURL *curl = nullptr;
CURLcode res;
struct FtpFile ftpfile={
strfilepath.c_str(), 
nullptr
};


curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();                       
if(curl) { 
char errbuf[CURL_ERROR_SIZE];
set_share_handle(curl);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(curl, CURLOPT_URL,strurl.c_str());

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); 
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);      

curl_easy_setopt(curl, CURLOPT_POST, 0);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);

curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, sstrCookiePath.c_str()); 
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, sstrCookiePath.c_str()); 


res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
LOG_ALL_ERROR(QString(errbuf));
}
else
{
bret = true;
}

curl_easy_cleanup(curl);
curl_global_cleanup();
}

if(ftpfile.stream)
fclose(ftpfile.stream);
return bret;
}

2.post方式发送数据

   url格式:服务器url地址+ 接口url地址

数据发送:

curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strpostdata.c_str());
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,strpostdata.size());

参考代码如下:post方式下载文件并传递数据

  bool cathttpclient::DownloadFile(IN const std::string &stshortrurl, IN const std::string &strshortdata, 
                            IN const std::string &strfilepath)
{
bool bret = false;
if (stshortrurl.empty() || strfilepath.empty())
{
LOG_ALL_ERROR(QString("failed download param is empty."));
return bret;
}


std::string strurl(m_strServerUrl + stshortrurl);
std::string strpostdata("");
GetPacketInfo(strshortdata, strpostdata);

CURL *curl = nullptr;
CURLcode res;
struct FtpFile ftpfile={
strfilepath.c_str(), 
nullptr
};

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();                       
if(curl) { 
char errbuf[CURL_ERROR_SIZE];
set_share_handle(curl);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(curl, CURLOPT_URL,strurl.c_str());

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); 
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);      


curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strpostdata.c_str());
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,strpostdata.size());

curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);


curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, sstrCookiePath.c_str()); 
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, sstrCookiePath.c_str()); 


res = curl_easy_perform(curl);
if (res != CURLE_OK)
LOG_ALL_ERROR(QString(errbuf));

bret = res == CURLE_OK;
curl_easy_cleanup(curl);
curl_global_cleanup();
}

if(ftpfile.stream)
fclose(ftpfile.stream);
return bret;
}


猜你喜欢

转载自blog.csdn.net/llfwdd/article/details/48495149