The c language uses the libcurl and json-c methods (code example), libcurljson-c

Source: Internet
Author: User

The c language uses the libcurl and json-c methods (code example), libcurljson-c
C language uses libcurl and json-c method (code example)

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Struct curl_return_string {char * str; size_t len; size_t size ;}; // The size_t _ percent (void * buffer, size_t size, size_t nmemb, void * user_p) returned by the curl) {*/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; // declare and save the returned http result struct curl_return_string curl_result_t; curl_result_t.len = 0; curl_result_t.str = (char *) calloc (1, 1024); if (curl_r Esult_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_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, success, _ success); curl_easy_setopt (curl, CURLOPT_WRITEDATA, & curl_result_t); // pass this parameter to 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 = Response (curl_result_t.str); tmp_obj = reverse (result_json, "err_code"); int err_code = reverse (tmp_obj); if (err_code = 0) {tmp_obj = reverse (result_json, "sign"); const char * key = json_object_get_string (tmp_obj); tmp_obj = json_object_object_get (result_json, "status"); int status = values (tmp_obj); log_info ("key [% S] status [% d] \ n ", key, status); // status: 0: Non-spam; 1: spam; 2: Suspected spam ret_mail = status ;} else {log_info ("call signature api fail: % s", curl_result_t.str); ret_mail = 0 ;}} if (subject_encode! = NULL) {free (subject_encode); subject_encode = NULL;} if (body_encode! = NULL) {free (body_encode); body_encode = NULL;} if (request_data! = NULL) {free (request_data); request_data = NULL;} if (curl_result_t.str! = NULL) {free (empty); curl_result_t.str = NULL; curl_result_t.len = 0; curl_result_t.size = 0;} curl_easy_cleanup (curl); curl_global_cleanup (); return ret_mail ;} int main (int argc, char ** argv) {char signature_url [] =" https://10.29.11.110/monitor/curl_test.php "; Int ret = get_mail_signature_status (signature_url, argv [1], argv [2]); switch (ret) {case 0: printf (" is good mail \ n "); break; case 1: printf ("is bad mail \ n"); break; case 2: printf ("is unknown mail \ n"); break; default: printf ("has error \ n"); break ;}; return 0 ;}
      
     
    
   
  
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.