{"id":939,"date":"2024-08-05T22:15:07","date_gmt":"2024-08-05T14:15:07","guid":{"rendered":"http:\/\/www.madbull.site\/?p=939"},"modified":"2024-08-05T22:23:56","modified_gmt":"2024-08-05T14:23:56","slug":"libcurl%e5%ae%9e%e7%8e%b0post%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=939","title":{"rendered":"libcurl\u5b9e\u73b0POST\u8bf7\u6c42"},"content":{"rendered":"\n<p>\u9700\u8981\u4e09\u4e2a\u6587\u4ef6\uff0c\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<div style=\"height:23px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"403\" height=\"256\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-2.png\" alt=\"\" class=\"wp-image-940\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-2.png 403w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-2-300x191.png 300w\" sizes=\"auto, (max-width: 403px) 100vw, 403px\" \/><\/figure>\n\n\n\n<div style=\"height:23px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>httpclient.c \u5b9e\u73b0POST\u8bf7\u6c42\u7684\u5c01\u88c5<\/p>\n\n\n\n<p>httpclient.h \u7528\u4e8e\u5bf9\u5916\u63d0\u4f9b\u63a5\u53e3<\/p>\n\n\n\n<p>test.c \u662f\u6d4b\u8bd5\u6587\u4ef6\uff0c\u6d4b\u8bd5POST\u80fd\u5426\u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n\n\n\n<div style=\"height:23px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>httpclient.c\u6587\u4ef6\uff0c\u5c01\u88c5libcurl\u7684POST\u8bf7\u6c42\u63a5\u53e3\uff0c<\/strong><strong>\u5b9e\u73b0POST\u53d1\u9001\uff0c\u5e76\u83b7\u53d6content\u6570\u636e\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;stdio.h>\n#include&lt;stdlib.h>\n#include&lt;string.h>\n#include &lt;curl\/curl.h>\n#include &lt;curl\/easy.h>\n\n#include \"httpclient.h\"\n\n\n\/*\n * post\u8bf7\u6c42\u7684\u8fd4\u56de\u7ed3\u679c\u56de\u8c03\u51fd\u6570\n * \u628acontent\u7684\u6570\u636ecopy\u5230userdata\u7ed9\u5b9a\u7684\u7a7a\u95f4\n * \u6ce8\u610f\uff1a\u9700\u8981\u5728\u5916\u90e8\u91ca\u653e\u7533\u8bf7\u7684 userdata.data \u7a7a\u95f4\n *\/\nsize_t post_callback(void* contents, size_t size, size_t nmemb, void* userdata) {\n    \/\/ \u5224\u65adcontents\u662f\u5426\u8fd4\u56de\u4e86\u7a7a\u503c\n    if (contents == NULL) {\n        return 0;\n    }\n\n    \/\/ \u4e00\u6b21\u56de\u8c03\u8fd4\u56de\u7684\u6570\u636e\u91cf\n    size_t realsize = size * nmemb;  \n    if (userdata == NULL) {\n        return realsize ;\n    }\n\n    \/\/ \u7c7b\u578b\u8f6c\u6362\n    http_resp_data * resp_data = (http_resp_data *) userdata;\n\n    resp_data->data= (char*)calloc(1, realsize );\n    if (resp_data->data== NULL) {\n        printf(\"not enough memory (realloc returned NULL)\\n\");\n        return 0;\n    }\n    memcpy(resp_data->data, contents, realsize);\n    resp_data->size = realsize;\n\n    return realsize;  \/\/\u5fc5\u987b\u8fd4\u56de\u771f\u5b9e\u7684\u6570\u636e\n}\n\n\n#define MAX_URL_LEN 256\n\n\n\/*\n * \u793a\u4f8b \uff1a\n * proto : http  or   https\n * host : 127.0.0.1:8080   host:port\n * method : \/xxx\/yyy\n * data : {\"aa\":\"aaaaaaaaaaa\",\"bb\",\"bbbbbbbbbbbbbbbb\", .... }\n * size : data\u7684\u5b57\u8282\u6570\n * user_data : \u56de\u8c03\u51fd\u6570\u5f85\u5904\u7406\u7684\u6570\u636e\n *\/\nint http_post_by_json(const char * proto, const char* host, const char* method, const char* data, size_t size, http_resp_data* user_data) {\n    int ret = 0;\n    \/\/ 1\u3001\u521d\u59cb\u5316\n    CURL* curl = curl_easy_init();\n    if (curl == NULL) {\n        printf(\"curl easy init\");\n        ret = -1;\n        goto over;\n    }\n\n    \/\/ 2\u3001\u62fc\u5199\u5b8c\u6574\u7684URL\n    char new_url&#91;MAX_URL_LEN] = {0} ;\n    if (method&#91;0] == '\/') {\n        sprintf(new_url, \"%s:\/\/%s%s\", proto, host, method );\n    } else {\n        sprintf(new_url, \"%s:\/\/%s\/%s\", proto, host, method);\n    }\n\n    \/\/ 3\u3001\u8bbe\u7f6e\u53c2\u6570\n    \/\/ 3.1\u3001\u8bbe\u7f6e\u4f7f\u7528POST\u65b9\u6cd5(\u9ed8\u8ba4\u4f7f\u7528GET\u65b9\u6cd5\u53d1\u9001\u8bf7\u6c42)\uff0c\n    curl_easy_setopt(curl, CURLOPT_POST, 1);\n    \/\/ \u8fd4\u56de\u5b8c\u6574\u7684HTTP\u54cd\u5e94\u5934\n    curl_easy_setopt(curl, CURLOPT_HEADER, 1);\n\n    \/\/ 3.2\u3001header.Content-Type \u548c header.Accept \u4fe1\u606f\n    struct curl_slist* headers = NULL;\n    headers = curl_slist_append(headers, \"Content-Type: application\/json\") ;\n    headers = curl_slist_append(headers, \"Accept: *\/*\") ;\n\n    \/\/ 3.3\u3001\u8bbe\u7f6e header.Host\n    char host_header&#91;MAX_URL_LEN] = {0};\n    sprintf(host_header, \"Host: %s\", host);\n    headers = curl_slist_append(headers, host_header);\n\n    \/\/ 3.4\u3001\u8bbe\u7f6eheader\u548cbody\n    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);\n    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);\n    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, size);\n\n    \/\/ 3.5\u3001\u8bbe\u7f6e\u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4\n    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);\n\n    \/\/ 3.6\u3001\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\n    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, post_callback);\n\n    \/\/ 3.7\u3001\u5141\u8bb8\u91cd\u5b9a\u5411\u5730\u5740\n    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1) ;\n\n    \/\/ 3.8\u3001\u8bbe\u7f6eurl\u5730\u5740\n    curl_easy_setopt(curl, CURLOPT_URL, new_url);\n\n    \/\/ 3.9\u3001\u8bbe\u7f6e\u5411\u56de\u8c03\u51fd\u6570\u4f20\u9012\u7684\u6570\u636e\n    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)user_data);\n\n    \/\/ 4\u3001\u53d1\u8d77\u8bf7\u6c42\n    CURLcode res = curl_easy_perform(curl);\n    if (res != CURLE_OK) {\n        const char* error = curl_easy_strerror(res);\n        printf(\"http post request failure, url=%s curl code=%d error=%s\", new_url, res, error);\n        ret = -1;\n        goto over;\n    } \n    \/\/ 5\u3001\u5224\u65ad\u8fd4\u56de\u7801\n    long http_code = 0;\n    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &amp;http_code);\n    if (http_code >= 400) {\n        printf(\"http post request failure, url=%s curl code=%ld\", new_url, http_code) ;\n        ret = -1;\n        goto over;\n    } else {\n        printf(\"success http_code=%ld\\n\", http_code) ;\n    }\n\nover :\n    curl_easy_cleanup(curl);\n    return ret;\n}<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>httpclient.h\u6587\u4ef6\uff0c\u4e0a\u65b9\u4ee3\u7801\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff0c\u7528\u4e8e\u5bf9\u5916\u66b4\u9732\u63a5\u53e3\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifndef __HTTP_CLIENT_H__\n#define __HTTP_CLIENT_H__\n#include &lt;stdio.h>\n\ntypedef struct http_resp_data_t {\n    char * data;\n    size_t size;\n} http_resp_data;\n\n\n\/*\n * \u793a\u4f8b \uff1a\n * proto : http  or   https\n * host : 127.0.0.1:8080   host:port\n * method : \/xxx\/yyy\n * data : {\"aa\":\"aaaaaaaaaaa\",\"bb\",\"bbbbbbbbbbbbbbbb\", .... }\n * size : data\u7684\u5b57\u8282\u6570\n * user_data : \u56de\u8c03\u51fd\u6570\u5f85\u5904\u7406\u7684\u6570\u636e\n *\/\nint http_post_by_json(const char * proto, const char* host, const char* method, const char* data, size_t size, http_resp_data* user_data) ;\n\n#endif<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>\u8c03\u7528\u7a0b\u5e8ftest.c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;stdio.h>\n#include&lt;stdlib.h>\n#include&lt;string.h>\n\n#include \"httpclient.h\"\n\n\nint main() {\n    http_resp_data resp_data ;\n    memset(&amp;resp_data, 0x00, sizeof(http_resp_data)) ;\n    char * data = \"{\\\"name\\\":\\\"madbull.site\\\", \\\"tel\\\":\\\"12312312311\\\", \\\"email\\\":\\\"aaa@ccc.com\\\"}\" ;    \n    http_post_by_json(\"http\", \"127.0.0.1:3322\", \"\/test\/t1\", data, strlen(data), &amp;resp_data) ;\n    if(resp_data.data != NULL) {\n        printf(\"resp_data.data &#91;%s]\\n\", resp_data.data) ;\n        free(resp_data.data) ;\n        memset(&amp;resp_data, 0x00, sizeof(http_resp_data)) ;\n    }\n\n    return 0 ;\n}<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<p>\u672c\u6b21\u6d4b\u8bd5\u7528\u6765 \u4e4b\u524d\u4f7f\u7528go\u642d\u5efa\u7684 POST\u8bf7\u6c42\uff0c\u53ef\u4ee5\u7ffb\u770b <a href=\"https:\/\/www.madbull.site\/?p=749\">https:\/\/www.madbull.site\/?p=749<\/a> \u642d\u5efa\u8d77http\u670d\u52a1\u5668\u3002\u670d\u52a1\u6b63\u5e38\u542f\u52a8\u540e\uff0c\u53ef\u6309\u7167\u4e0b\u8fb9\u7684\u65b9\u6cd5\u7f16\u8bd1\u548c\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<div style=\"height:23px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"186\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-3-1024x186.png\" alt=\"\" class=\"wp-image-941\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-3-1024x186.png 1024w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-3-300x54.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-3-768x139.png 768w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-3-1536x278.png 1536w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-3.png 1838w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u8981\u4e09\u4e2a\u6587\u4ef6\uff0c\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\u56fe\uff1ahttpclient.c \u5b9e\u73b0POST\u8bf7\u6c42\u7684\u5c01\u88c5\uff1bhttpclient.h \u7528\u4e8e\u5bf9\u5916\u63d0\u4f9b\u63a5\u53e3\uff1btest.c \u662f\u6d4b\u8bd5\u6587\u4ef6\uff0c\u6d4b\u8bd5POST\u80fd\u5426\u53d1\u9001\u8bf7\u6c42\u3002httpclient.c\u6587\u4ef6\uff0c\u5c01\u88c5libcurl\u7684POST\u8bf7\u6c42\u63a5\u53e3\uff0c\u5b9e\u73b0POST\u53d1\u9001\uff0c\u5e76\u83b7\u53d6content\u6570\u636e\u3002<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[378,158,175,299,379,377,300],"class_list":{"0":"post-939","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-c-c","8":"category-154","9":"tag-c","10":"tag-curl","12":"tag-http","13":"tag-httpclient","14":"tag-libcurl","15":"tag-post"},"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=939"}],"version-history":[{"count":5,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/939\/revisions"}],"predecessor-version":[{"id":946,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/939\/revisions\/946"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}