基于libcurl的POST(http)
#include <stdio.h> #include <curl/curl.h> int main (void) { char *url="http://www.nengyouyun.cn/user/getAppversionnew2?apptype=H5C899DDC"; //char *url="http://127.0.0.1:8080"; //Liuzhenbo //char *url="http://www.baidu.com"; //Liuzhenbo //char *url="localhost"; CURL *pCurl = NULL; CURLcode res; struct curl_slist *headerlist = NULL; struct curl_httppost *post = NULL; struct curl_httppost *last = NULL; //-----------------------------------------------------------------------//picture1 curl_formadd(&post, &last,CURLFORM_COPYNAME, "picture1", //此处表示要传的参数名 CURLFORM_FILE, "1.jpg", //此处表示图片文件的路径 //CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END); //------------------------------------------------------------------------//picture2 curl_formadd(&post, &last, CURLFORM_COPYNAME, "picture2", //此处表示要传的参数名 CURLFORM_FILE, "2.jpg", //此处表示图片文件的路径 CURLFORM_END); //----------------------------------------------------------------------------//json curl_formadd(&post, &last, CURLFORM_COPYNAME, "text1", //此处为别的参数 CURLFORM_COPYCONTENTS, "1.json", //要上传的json字符串 CURLFORM_END); //------------------------------------------------------------------------------ pCurl = curl_easy_init(); //初始化句柄 -liuzhenbo if (NULL != pCurl) { //curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 5); curl_easy_setopt(pCurl, CURLOPT_URL, url); curl_easy_setopt(pCurl, CURLOPT_HTTPPOST, post); res = curl_easy_perform(pCurl); // printf("\nres is OK!--liuzhenbo\n"); //-liuzhenbo if (res != CURLE_OK) { printf("\nres is not ok!--liuzhenbo\n"); //-liuzhenbo printf("curl_easy_perform() failed,error code is:%s\n", curl_easy_strerror(res)); } printf("\n"); curl_easy_cleanup(pCurl); } }
服务器返回的http主体:
lzb@lzb:~/projects/curls/http_post$ ./liu_post {"app":{"id":96,"version":"1.73","upfile":"3957","apkfile":null,"apptype":{"id":"apptype-H5C899DDC","key":{"dicno":"apptype","datano":"H5C899DDC"},"name":"昱昌","sortnum":"2","isuse":"2"},"descr":"修改APP地图界面客户数的统计"}}