码迷,mamicode.com
首页 > Web开发 > 详细

curl向web服务器发送json数据

时间:2015-10-23 11:44:37      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

curl向web服务器发送json数据

标签:

原文地址:http://www.cnblogs.com/chris-cp/p/4903847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!