设为首页 加入收藏

TOP

C语言使用libcurl与json-c方法(代码实例)(一)
2017-12-07 14:22:06 】 浏览:58
Tags:语言 使用 libcurl json-c 方法 代码 实例

C语言使用libcurl与json-c方法(代码实例)

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         struct curl_return_string { char *str; size_t len; size_t size; }; // 用于存curl返回的结果 size_t _recive_data_from_http_api(void *buffer, size_t size, size_t nmemb, void *user_p) { */ struct curl_return_string *result_t = (struct curl_return_string *)user_p; if (result_t->size < ((size * nmemb) + 1)) { result_t->str = (char *)realloc(result_t->str, (size * nmemb) + 1); if (result_t->str == NULL) { return 0; } result_t->size = (size * nmemb) + 1; } result_t->len = size * nmemb; memcpy(result_t->str, buffer, result_t->len); result_t->str[result_t->len] = '\0'; return result_t->len; } int get_mail_signature_status(char *url, char *subject, char *body) { int ret_mail = 0; // 声明保存返回 http 的结果 struct curl_return_string curl_result_t; curl_result_t.len = 0; curl_result_t.str = (char *)calloc(1, 1024); if (curl_result_t.str == NULL) { log_error("calloc fail for curl_result_t.str"); return 0; } curl_result_t.size = 1024; curl_global_init(CURL_GLOBAL_ALL); CURL *curl; CURLcode ret; // init curl curl = curl_easy_init(); if (!curl) { log_error("couldn't init curl"); return 0; } curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_POST, 1); // use post // urlencode post data char *subject_encode = curl_easy_escape(curl, subject, strlen(subject)); if (!subject_encode) { log_error("urlencode subject fail, so use source data"); subject_encode = subject; } char *body_encode = curl_easy_escape(curl, body, strlen(body)); if (!body_encode) { log_error("urlencode body fail, so use source data"); body_encode = body; } int request_data_len = 18 + strlen(subject_encode) + strlen(body_encode) + 2; char *request_data = (char *)calloc(1, request_data_len) ; if (request_data == NULL) { log_error("calloc fail for request_data"); curl_easy_cleanup(curl); curl_global_cleanup(); return 0; } snprintf(request_data, request_data_len, "subject=%s&content=%s", subject_encode, body_encode); log_debug("request data:%s\n", request_data); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _recive_data_from_http_api); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &curl_result_t); // 传这个参数过去 ret = curl_easy_perform(curl); if(ret != CURLE_OK) { log_error("curl_easy_perform() failed:%s, url:%s", curl_easy_strerror(ret), url); } else { if (curl_result_t.str) { log_info("request url:%s response:%s", url, curl_result_t.str); // parse result with json // {"err_code":200,"err_msg": "this is no error", "sign":11281921309418229, "status": 1}' struct array_list *tmp_array; struct json_object *tmp_obj; struct json_object *result_json; result_json = json_tokener_parse(curl_result_t.str); tmp_obj = json_object_object_get(result_json, "err_code"); int err_code = json_object_get_int(tmp_obj); if (err_code == 0) { tmp_obj = json_object_object_get(result_json, "sign"); const char *key = json_object_get_string(tmp_obj); tmp_obj = json_object_object_get(result_json, "status"); int status = json_object_get_int(tmp_obj); log_info("key[%s] status[%d]\n", ke
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇数据结构之插入排序和希尔排序实.. 下一篇C语言指向函数的指针运行讲解

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

最新文章

热门文章

C 语言

C++基础

windows编程基础

linux编程基础

C/C++面试题目