通过libcurl向服务器发送数据,返回的数据说明

1.返回一块内存数据

    若数据为空,则不会回调函数WriteMemoryCallback,也就不会写入内存chunk

    若数据不为空,则回调函数WriteMemoryCallback,写入内存chunk

  代码如下:

  

bool cathttpclient::HttpPost(IN const std::string &strshorturl,IN const std::string&strshortdata, OUTstd::string&strrecvdata)

{

         if (m_strServerUrl.empty())

                   returnfalse;

 

         std::string strurl("");

         strurl= m_strServerUrl + strshorturl;

 

         std::string strpostdata("");

         if (!GetPacketInfo(strshortdata,strpostdata))

                   returnfalse;

 

         //LOG_ALL_ERROR(QString("httppost:")+ QString(strpostdata.c_str()));

         CURLcoderes;

        

         //会不会是问题出在这里

         curl_global_init(CURL_GLOBAL_ALL);

         CURL*curl = curl_easy_init();

         //set_share_handle(curl);

         if(NULL == curl)

                   returnfalse;

 

         std::string strutf8("");

         struct MemoryStruct chunk;

         chunk.memory = (char *)malloc(1); /* will be grown as needed by the reallocabove */

         chunk.size = 0;    /* no data at this point */

 

         curl_easy_setopt(curl, CURLOPT_URL,strurl.c_str());

         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);

 

         /* send alldata to this function  */

         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,WriteMemoryCallback);

 

         /* we passour 'chunk' struct to the callback function */

        

         curl_easy_setopt(curl, CURLOPT_WRITEDATA,(void *)&chunk);

 

         //curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, OnWriteData);

         //curl_easy_setopt(curl,CURLOPT_WRITEDATA, (void *)&strutf8);

 

         curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1);

         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,100000);

         curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS,100000);

 

         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)

                   returnres;

         else

         {

                   strutf8.append(chunk.memory, chunk.size);

                   Q_ASSERT(strutf8.size()== chunk.size);

         }

         curl_easy_cleanup(curl);

 

         free(chunk.memory);

         curl_global_cleanup();

         //strrecvdata= strutf8;

         return res == CURLE_OK&& Utf8TransGb18030(strutf8, strrecvdata);

}

2.返回一个文件

    若数据为空,则不会回调函数my_fwrite,也就不会写入文件ftpfile

   若数据不为空,则回调函数my_fwrite,写入文件ftpfile

代码如下:

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("faileddownload param is empty."));

                   returnbret;

         }

 

         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;

         CURLcoderes;

         struct FtpFile ftpfile={

                   strfilepath.c_str(),

                   nullptr

         };

 

         curl_global_init(CURL_GLOBAL_ALL);

         curl = curl_easy_init();                      

         if(curl) {

                   charerrbuf[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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值