libcurl API:无法完全接收服务器数据问题
来源:互联网 发布:旺宝淘宝收藏软件 编辑:程序博客网 时间:2024/06/08 11:10
1、用下面的代码如果服务器返回数据较大的话无法全部接收:
#include <stdio.h>#include <curl/curl.h>#include <stdlib.h>#include<string.h>#include <sys/types.h>#include <json/json.h>#include<string>size_t saveData(void *ptr, size_t size, size_t nmemb, void *userdata){sprintf((char *)userdata,"%s",(char *)ptr);}int main(int argc, char *argv[]){CURL *curl;CURLcode res;//char data[]="{\"method\" : \"xbmc_get_dir\",\"para\" : \"?mode=1&name=%E7%94%B5%E8%A7%86%E5%89%A7&id=c_97&cat=%E4%B8%8D%E9%99%90&area=%E4%B8%8D%E9%99%90&year=%E4%B8%8D%E9%99%90&order=7\"}";//char data[]="{\"method\" : \"xbmc_get_dir\",\"para\" : \"\"}";json_object *p_json_obj = NULL;p_json_obj = json_tokener_parse(data);int len;curl = curl_easy_init();char rece[20480];if(curl!=NULL){curl_easy_setopt(curl, CURLOPT_POST, 1L);curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1/xbmc-server.php");curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(data));curl_easy_setopt(curl, CURLOPT_WRITEDATA, rece);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, saveData);res = curl_easy_perform(curl);curl_easy_cleanup(curl);}printf("111%s\n",rece);json_object *p_json_obj1 = NULL;p_json_obj1 = json_tokener_parse(rece);json_object *p_obj = json_object_object_get(p_json_obj1, "dirlist");char * p_method;p_method = (char *)json_object_get_string(p_obj);printf("%s\n",p_method);return 0;}
2、改用如下可以:
#include <stdio.h>#include <curl/curl.h>#include <stdlib.h>#include<string.h>#include <sys/types.h>#include <json/json.h>#include<string>std::string strrr;size_t saveData(void *ptr, size_t size, size_t nmemb, void *userdata){char httpsBuffer[10240];printf("22222222%s\n",userdata);printf("3333333%s\n\n\n\n",ptr);memset (httpsBuffer,0,sizeof(httpsBuffer));memcpy(httpsBuffer,ptr,nmemb);std::string tmp((char *)httpsBuffer);strrr+=tmp;return nmemb*size;}int main(int argc, char *argv[]){CURL *curl;CURLcode res;//char data[]="{\"method\" : \"xbmc_get_dir\",\"para\" : \"?mode=1&name=%E7%94%B5%E8%A7%86%E5%89%A7&id=c_97&cat=%E4%B8%8D%E9%99%90&area=%E4%B8%8D%E9%99%90&year=%E4%B8%8D%E9%99%90&order=7\"}";json_object *p_json_obj = NULL;p_json_obj = json_tokener_parse(data);int len;curl = curl_easy_init();char rece[20480];if(curl!=NULL){curl_easy_setopt(curl, CURLOPT_POST, 1L);curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1/xbmc-server.php");curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(data));curl_easy_setopt(curl, CURLOPT_WRITEDATA, rece);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, saveData);res = curl_easy_perform(curl);curl_easy_cleanup(curl);}printf("000%s\n",strrr.c_str());json_object *p_json_obj1 = NULL;p_json_obj1 = json_tokener_parse(strrr.c_str());json_object *p_obj = json_object_object_get(p_json_obj1, "dirlist");char * p_method;p_method = (char *)json_object_get_string(p_obj);printf("%s\n",p_method);return 0;}
- libcurl API:无法完全接收服务器数据问题
- libcurl 上传测试 php 服务器接收
- GSM模块无法接收数据问题
- 服务器无法接收邮件
- 为什么TIdTcpClient总是无法接收服务器发来的数据?
- libcurl Get json 数据 接收全部的数据
- UDP数据接收服务器
- DM9000A驱动调试(解决网卡无法接收数据问题)
- 解决VS2010使用mscomm控件无法接收数据的问题
- GCDAsyncSocket接收数据连包无法解析的问题
- bootstrap table post提交数据,后台无法接收的问题
- 无法接收邮件问题
- Android部分手机接收服务器数据显示乱码问题
- jqueryeasyui datagrid column数据过长无法完全展示问题
- php 无法接收post数据
- Mscomm接收数据问题
- Lua数据接收问题
- libcurl下载数据不全的问题
- 斯坦福机器学习公开课笔记(三)--逻辑回归
- nginx tomcat 配置集群负载
- js函数调用直接打印功能
- Total Commander 常用快捷键(并附快捷键大全)
- python unicode bytes ascii utf-8
- libcurl API:无法完全接收服务器数据问题
- WIN8换WIN7的经验分享
- pyinstaller--将python打包成exe
- distcc
- ubuntu下修改path三种方法
- maven配置: 生成war的同时生成jar, 并在其他artifact配置依赖于这个jar
- 使用指定的文件浏览器查看指定文件夹里的图片
- 问题:shell脚本while中的more和cat
- 苹果推送通知服务(APNs)编程