curl向web服务器发送json数据

简介: c++使用libcurl: 1 /* 2 *g++ demo.cpp -g -Wall -lcurl 3 */ 4 5 #include 6 #include 7 #include 8 #include 9 #include ...

c++使用libcurl:

  1 /*
  2  *g++ demo.cpp  -g -Wall -lcurl
  3  */
  4 
  5 #include <string.h>
  6 #include <stdlib.h>
  7 #include <stdio.h>
  8 #include <iostream>
  9 #include <curl/curl.h>
 10 
 11 int getUrl(const char* filename)
 12 {
 13     CURL* curl;
 14     CURLcode res;
 15     FILE* fp; 
 16     if((fp=fopen(filename, "w"))==NULL){
 17         return -1; 
 18     }   
 19     struct curl_slist *headers = NULL;
 20     headers = curl_slist_append(headers, "Accept:Agent-007");
 21     curl = curl_easy_init();    
 22     if(curl){
 23         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
 24         curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com");
 25         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
 26         curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp);
 27 
 28         //0: success, other: failure
 29         res = curl_easy_perform(curl);
 30         if(res != 0){ 
 31             std::cout <<"error:"<<curl_easy_strerror(res) << std::endl;
 32         }
 33         curl_slist_free_all(headers);
 34         curl_easy_cleanup(curl);
 35         fclose(fp);
 36     }
 37     return res;
 38 }
 39 
 40 /*
 41 int postUrl(const char* filename)
 42 {
 43     CURL* curl;
 44     CURLcode res;
 45     FILE* fp;
 46 
 47     if((fp=fopen(filename, "w"))==NULL){
 48         return 1;
 49     }
 50     curl = curl_easy_init();
 51     if(curl){
 52     
 53     }
 54 }
 55 */
 56 
 57 //typedef  int  (* func)(int,  int);
 58 typedef  size_t (*cb_func)(void*, size_t, size_t, void*);
 59 size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
 60     FILE *fptr = (FILE*)userp;
 61     fwrite(buffer, size, nmemb, fptr);
 62     return size*nmemb;
 63 }
 64 
 65 int PostData(const char* postdata, const char* url,
 66              cb_func write_data, void* fptr)
 67 //int PostData(const char* postdata, const char* url)
 68 {
 69     CURL* curl = NULL;
 70     CURLcode res;
 71     char tmp[32] = {0};
 72     struct curl_slist* headers = NULL;
 73     if(!url){
 74         return -1;
 75     }
 76     std::cout <<"send data url:" << url << std::endl;
 77     snprintf(tmp, sizeof(tmp), "Content-Length: %d", (int)strlen(postdata));
 78     std::cout <<"Content-Length: " << tmp << std::endl;
 79     headers = curl_slist_append(headers, "Accept: application/json");
 80     headers = curl_slist_append(headers, "Content-Type: application/json");
 81     headers = curl_slist_append(headers, "charset: utf-8");
 82     headers = curl_slist_append(headers, tmp);
 83 
 84     curl_global_init(CURL_GLOBAL_ALL);
 85     curl = curl_easy_init();
 86     if(curl){
 87         curl_easy_setopt(curl, CURLOPT_URL,url);
 88         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
 89         curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1); //超时时间1s
 90         curl_easy_setopt(curl, CURLOPT_POST, 1L);
 91         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
 92         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
 93         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr);
 94         //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);   //debug 
 95         res = curl_easy_perform(curl);
 96         if(res!=0){
 97             std::cout<<"error no:"<<curl_easy_strerror(res)<<std::endl;
 98         }
 99         curl_slist_free_all(headers);
100         curl_easy_cleanup(curl);
101         return res;
102     }else{
103         curl_slist_free_all(headers);
104         return -2;
105     }
106 }
107 
108 int main()
109 {
110     const char* tt = "./aa.out";
111     int aa =  getUrl(tt);
112     std::cout << "return :" << aa << std::endl;
113     FILE* fptr;
114     if((fptr=fopen("./result", "w"))==NULL){
115         std::cout << "file open error" << std::endl;
116         exit(1);
117     }
118 
119     const char* jsondata = "{\"usrname\": \"cp\", \"passwd\": \"test\"}";
120      const char* url = "http://192.168.2.163:8080/mystorage/mytest.php";
121     aa = PostData(jsondata, url, write_data, fptr);
122     return 0;
123 }

 

相关文章
|
28天前
|
JSON 前端开发 Java
Json格式数据解析
Json格式数据解析
|
4天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
13 0
|
6天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
12 1
|
6天前
|
JSON 数据格式 Python
Python处理JSON数据
【4月更文挑战第30天】该内容介绍了Python处理JSON数据的三个方法:1)使用`json.loads()`尝试解析字符串以验证其是否为有效JSON,通过捕获`JSONDecodeError`异常判断有效性;2)通过`json.dumps()`的`indent`参数格式化输出JSON数据,使其更易读;3)处理JSON中的日期,利用`dateutil`库将日期转换为字符串进行序列化和反序列化。
16 4
|
7天前
|
XML JSON 前端开发
【Web 前端】XML和JSON的区别?
【4月更文挑战第22天】【Web 前端】XML和JSON的区别?
【Web 前端】XML和JSON的区别?
|
9天前
|
存储 JSON 数据处理
|
11天前
|
JSON 数据可视化 定位技术
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
16 0
|
19天前
|
JSON JavaScript 数据格式
vue展示json数据,vue-json-viewer的使用
vue展示json数据,vue-json-viewer的使用
25 0
|
24天前
|
存储 JSON JavaScript
「Python系列」Python JSON数据解析
在Python中解析JSON数据通常使用`json`模块。`json`模块提供了将JSON格式的数据转换为Python对象(如列表、字典等)以及将Python对象转换为JSON格式的数据的方法。
33 0
|
27天前
|
存储 JSON 数据挖掘
python逐行读取txt文本中的json数据,并进行处理
Python代码示例演示了如何读取txt文件中的JSON数据并处理。首先,逐行打开文件,然后使用`json.loads()`解析每一行。接着,处理JSON数据,如打印特定字段`name`。异常处理包括捕获`JSONDecodeError`和`KeyError`,确保数据有效性和字段完整性。将`data.txt`替换为实际文件路径运行示例。
14 2

热门文章

最新文章