CURL发送HTTP请求获得Json数据串保存在本地(linux 下 C++ 编程)
来源:互联网 发布:linux中怎么创建文件 编辑:程序博客网 时间:2024/06/05 09:07
最近在做一个项目,要用到Json和Curl请求。要求是这样的
我需要用CURL + 目的IP地址来向目的地址发送一个HTTP请求,请求会返回一个Json的字符串,我将其获得保存在本地用来使用;
我就直接上代码了,具体就不解释了,仅供大家参考
#include<iostream>#include<string>#include<curl/curl.h>#include<json/json.h>#include<fstream>#define filemax 2048#define CDN_num 1024using namespace std;size_t get_data(void*prt,size_t size, size_t nmemb, FILE * stream){size_t written = fwrite(prt,size,nmemb,stream);return written;}struct CDN_Info{string name;string IP;string APP;string StreamName;int Link_num;};int main(){CDN_Info CDN_Mess[CDN_num];int i=0;FILE* fp; const char* api_url="192.168.1.107/getclient";CURLcode res;CURL* curl=curl_easy_init();char outfile[filemax]="Temp.json";fp = fopen(outfile,"wb");curl_easy_setopt(curl,CURLOPT_URL,api_url);curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,get_data);curl_easy_setopt(curl,CURLOPT_WRITEDATA,fp);res=curl_easy_perform(curl);if (res!=CURLE_OK){perror("Curl error");return 1;}curl_easy_cleanup(curl);fclose(fp);Json::Reader reader;Json::Value root;ifstream is;is.open("Temp.json",ios::out); if (!reader.parse(is,root)){return 1;}for (Json::Value::iterator idx = root.begin();idx != root.end();idx++){CDN_Mess[i].name=root[i]["name"].asString();CDN_Mess[i].IP=root[i]["url"].asString();CDN_Mess[i].APP=root[i]["app"].asString();CDN_Mess[i].StreamName=root[i]["stream"].asString(); CDN_Mess[i].Link_num=root[i]["client"].asInt();i++;}}
阅读全文
0 0
- CURL发送HTTP请求获得Json数据串保存在本地(linux 下 C++ 编程)
- http请求远程接口获得JSON数据(1) --- 利用HttpURLConnection发送post/get请求
- http请求远程接口获得Json数据(2) --- 利用HttpClient发送post/get请求
- CURL发送HTTP请求
- curl 发送http 请求
- linux C发送 http 请求
- linux C发送 http 请求
- Linux下用c语言实现发送http请求
- linux中利用curl获得http请求的响应时间
- curl 模拟http发送请求
- 194.m1-json数据保存在本地
- http+json发送请求
- linux命令发送http的get或post请求(curl和wget两种方法)
- Python发送http请求(json格式)
- Linux下发送HTTP协议请求
- linux下发送http协议请求
- linux下使用shell发送http请求
- Linux/C-网络信息传输:cURL-HTTP协议发送信息
- win10中如何成功安装lxml
- Linux wext和nl80211接口简介
- 关于Autosar中DCM(14229UDS)模块的理解
- centos发行版视频解码问题的解决
- 单向链表-移动
- CURL发送HTTP请求获得Json数据串保存在本地(linux 下 C++ 编程)
- MySQL 修改字段类型或长度
- CodeForces
- 点击item将图片用Dialog以Viewpager形式展示出来,并放大缩小
- JAVA小结
- VMware安装Linux-Ubuntu系统(二)-安装vmware tools
- HDU6147(2017百度之星程序设计大赛
- Adroidstudio中创建数据库
- OGG:Oracle to MySQL异构复制环境搭建