{"id":998,"date":"2024-08-23T21:27:47","date_gmt":"2024-08-23T13:27:47","guid":{"rendered":"https:\/\/www.madbull.site\/?p=998"},"modified":"2024-10-21T11:52:19","modified_gmt":"2024-10-21T03:52:19","slug":"libcurl%e5%ae%9e%e7%8e%b0http%e5%ae%a2%e6%88%b7%e7%ab%afpost%e9%95%bf%e8%bf%9e%e6%8e%a5-keepalive","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=998","title":{"rendered":"libcurl\u5b9e\u73b0http\u5ba2\u6237\u7aefPOST\u957f\u8fde\u63a5&#8211;keepalive"},"content":{"rendered":"\n<p>http\u6bcf\u6b21\u8fde\u63a5\u6027\u80fd\u5f00\u9500\u975e\u5e38\u5927\uff0c\u5bf9\u4e8e\u670d\u52a1\u7aef\u53ef\u4ee5\u5feb\u901f\u8fd4\u56de\u7684\u8bf7\u6c42\uff0c<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-3-color\">\u957f\u8fde\u63a5\u53ef\u4ee5\u63d0\u9ad8\u51e0\u5341\u500d\u751a\u81f3\u4e0a\u767e\u500d\u7684\u5904\u7406\u6027\u80fd<\/mark><\/strong>\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u6b64\u9879\u76ee\u9700\u8981\u642d\u5efa\u4e00\u4e2a\u652f\u6301POST\u7684http\u6d4b\u8bd5\u670d\u52a1\u5668\uff0c\u53c2\u770b\u5f80\u671f\u6587\u7ae0\uff1a<a href=\"https:\/\/www.madbull.site\/?p=565\">https:\/\/www.madbull.site\/?p=565<\/a>\u3002\u6309\u7167\u6b65\u9aa4\u5c31\u53ef\u4ee5\u642d\u5efa\u8d77\u6765\u6d4b\u8bd5\u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u4e0d\u9700\u8981\u9ad8\u6027\u80fd\u5904\u7406\u7684\u670d\u52a1\uff0c\u4e5f\u5c31\u4e0d\u9700\u8981\u957f\u8fde\u63a5\uff0c\u53ef\u4ee5\u53c2\u770b\u5f80\u671f\u6587\u7ae0\uff1a<a href=\"https:\/\/www.madbull.site\/?p=939\">https:\/\/www.madbull.site\/?p=939<\/a>\uff0c\u5bf9\u4e8e\u8d44\u6e90\u7684\u7533\u8bf7\u91ca\u653e\u6bd4\u8f83\u65b9\u4fbf\uff0c\u4e5f\u4e0d\u7528\u8003\u8651\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\"><strong>\u6ce8\u610f\uff1a<\/strong><\/mark>\u6b64\u6b21\u793a\u4f8b\u5982\u679c\u7528\u5728\u5e76\u53d1\u4e2d\u4f7f\u7528\uff0ccurl\u53e5\u67c4\u662f\u4e34\u754c\u8d44\u6e90\uff0c\u9700\u8981\u505a\u597d\u4e92\u65a5\u8bbf\u95ee\uff0c\u6216\u8005\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u7533\u8bf7\u4e0d\u540c\u7684curl\u53e5\u67c4\uff0c\u4e5f\u53ef\u4ee5\uff0c\u9700\u8981\u7075\u6d3b\u5904\u7406\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:5px\" 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 class=\"has-large-font-size\"><strong>\u5934\u6587\u4ef6 httpclient.h<\/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&gt;\n#include &lt;curl\/curl.h&gt;\n\ntypedef struct http_resp_data_t {\n    char * data;\n    size_t size;\n} http_resp_data;\n\nCURL * create_keep_alive_post_curl() ;\n\nint http_post_by_json(CURL *curl, 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 class=\"has-large-font-size\"><strong>\u4ee3\u7801\u6587\u4ef6 httpclient.c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;curl\/curl.h&gt;\n#include &lt;curl\/easy.h&gt;\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-&gt;data= (char*)calloc(1, realsize );\n    if (resp_data-&gt;data== NULL) {\n        printf(\"not enough memory (realloc returned NULL)\\n\");\n        return 0;\n    }\n    memcpy(resp_data-&gt;data, contents, realsize);\n    resp_data-&gt;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 * \u521b\u5efa\u4e00\u4e2a\u957f\u8fde\u63a5\u7684curl\u53e5\u67c4\n *\/\nCURL * create_keep_alive_post_curl() {\n    \/\/ 1\u3001\u521d\u59cb\u5316\n    CURL* curl = curl_easy_init();\n    if (curl == NULL) {\n        printf(\"curl easy init failed!\\n\");\n        return NULL ;\n    }\n\n    \/\/ 2\u3001\u8bbe\u7f6e\u53c2\u6570\n    \/\/ 2.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\n    \/\/ 2.2\u3001\u8bbe\u7f6e\u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4\n    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);\n\n    \/\/ 2.3\u3001\u5141\u8bb8\u91cd\u5b9a\u5411\u5730\u5740\n    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1) ;\n\n    \/\/ 2.4\u3001\u8bbe\u7f6e keep-alive \u9009\u9879\n    curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L) ;\n    curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 60L) ;\n    curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 2L) ;\n    \/\/ curl_easy_setopt(curl, CURLOPT_TCP_KEEPCNT, 2L) ;\n\n    return curl ;\n}\n\n\/*\n * \u793a\u4f8b \uff1a\n * proto : http  or   https\n * host : 127.0.0.1:3456   host:port\n * method : \/xx\/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(CURL *curl, 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\n    \/\/ 1\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    \/\/ 2\u3001\u8bbe\u7f6eheader\n    \/\/ 2.1\u3001\u8bbe\u7f6eurl\u5730\u5740\n    curl_easy_setopt(curl, CURLOPT_URL, new_url);\n\n    struct curl_slist* headers = NULL;\n    \/\/ 2.2\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    \/\/ 2.3\u3001header.Content-Type\n    headers = curl_slist_append(headers, \"Content-Type: application\/json\") ;\n    \/\/ 2.4\u3001header.Accept \u4fe1\u606f\n    headers = curl_slist_append(headers, \"Accept: *\/*\") ;\n\n    \/\/ 2.5\u3001\u6dfb\u52a0header\u5217\u8868\n    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);\n\n\n    \/\/ 3\u3001\u8bbe\u7f6ebody\n    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);\n    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, size);\n\n    \/\/ 4\u3001\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\n    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, post_callback);\n\n    \/\/ 5\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    \/\/ 6\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\\n\", new_url, res, error);\n        ret = -1;\n        goto over;\n    } \n    \/\/ 7\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 &gt;= 400) {\n        printf(\"http post request failure, url=%s curl code=%ld\\n\", 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_slist_free_all(headers) ;\n    return ret;\n}\n\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 class=\"has-large-font-size\"><strong>\u6d4b\u8bd5\u6587\u4ef6 test.c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;curl\/curl.h&gt;\n\n#include \"httpclient.h\"\n\n\nint main(){\n    http_resp_data resp_data ;\n\n    memset(&amp;resp_data, 0x00, sizeof(resp_data)) ;\n\n    CURL * curl = create_keep_alive_post_curl() ;\n\n    \n    char * data1 = \"{\\\"aa\\\":\\\"aaaaaaaaaaaaaaa\\\"}\" ;\n    http_post_by_json(curl, \"http\", \"127.0.0.1:3456\", \"\/tt\/test\/t1\", data1, strlen(data1), &amp;resp_data) ;\n    printf(\"size &#91;%lu] message &#91;%s]\\n\", resp_data.size, resp_data.data) ;\n    free(resp_data.data) ;\n\n    memset(&amp;resp_data, 0x00, sizeof(resp_data)) ;\n    char * data2 = \"{\\\"bb\\\":\\\"bbbbbbbbbbbbbbb\\\"}\" ;\n    http_post_by_json(curl, \"http\", \"127.0.0.1:3456\", \"\/tt\/test\/t1\", data2, strlen(data2), &amp;resp_data) ;\n    printf(\"size &#91;%lu] message &#91;%s]\\n\", resp_data.size, resp_data.data) ;\n    free(resp_data.data) ;\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 class=\"has-large-font-size\"><strong>\u7f16\u8bd1\u53ca\u6d4b\u8bd5\u3002<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\u628a\u4e4b\u524d\u8bf4\u7684 http \u670d\u52a1\u8fd0\u884c\u8d77\u6765\uff0c\u4ee5\u5907\u6d4b\u8bd5\u3002\u6b64\u6b21\u628ahttpclient.c\u7f16\u8bd1\u6210\u52a8\u6001\u5e93\uff0c\u4f7f\u7528\u65f6\u66f4\u7075\u6d3b\u4e00\u4e9b\u3002<\/p>\n\n\n\n<p>\u6267\u884c export LD_LIBRARY_PATH=.\/ \u662f\u4e3a\u4e86\u7ed9\u5f53\u524d\u7ec8\u7aef\uff0c\u6dfb\u52a0\u8fd0\u884c\u65f6\u7684\u52a8\u6001\u5e93\u67e5\u627e\u8def\u5f84\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"217\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-7-1024x217.png\" alt=\"\" class=\"wp-image-1004\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-7-1024x217.png 1024w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-7-300x64.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-7-768x163.png 768w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-7.png 1187w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u901a\u8fc7\u6293\u5305\uff0c\u67e5\u770b\u662f\u5426\u771f\u7684\u662f\u957f\u8fde\u63a5\uff0c\u547d\u4ee4\u662f\uff1atcpdump -i lo port 3456 -w aa.pcap\uff0c\u6309ctrl+C\u7ed3\u675f\uff0c\u67e5\u770baa.pcap\u6587\u4ef6\u5373\u53ef\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:5px\" 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=\"620\" height=\"685\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/22-2.png\" alt=\"\" class=\"wp-image-1006\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/22-2.png 620w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/22-2-272x300.png 272w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>libcurl\u5b9e\u73b0http\u5ba2\u6237\u7aefPOST\u957f\u8fde\u63a5&#8211;keepalive\u3002http\u6bcf\u6b21\u8fde\u63a5\u6027\u80fd\u5f00\u9500\u975e\u5e38\u5927\uff0c\u5bf9\u4e8e\u670d\u52a1\u7aef\u53ef\u4ee5\u5feb\u901f\u8fd4\u56de\u7684\u8bf7\u6c42\uff0c\u957f\u8fde\u63a5\u53ef\u4ee5\u63d0\u9ad8\u51e0\u5341\u500d\u751a\u81f3\u4e0a\u767e\u500d\u7684\u5904\u7406\u6027\u80fd\u3002<\/p>\n<p>\u6b64\u9879\u76ee\u9700\u8981\u642d\u5efa\u4e00\u4e2a\u652f\u6301POST\u7684http\u6d4b\u8bd5\u670d\u52a1\u5668\uff0c\u53c2\u770b\u5f80\u671f\u6587\u7ae0\uff1ahttps:\/\/www.madbull.site\/?p=565\u3002\u6309\u7167\u6b65\u9aa4\u5c31\u53ef\u4ee5\u642d\u5efa\u8d77\u6765\u6d4b\u8bd5\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5bf9\u4e8e\u4e0d\u9700\u8981\u9ad8\u6027\u80fd\u5904\u7406\u7684\u670d\u52a1\uff0c\u4e5f\u5c31\u4e0d\u9700\u8981\u957f\u8fde\u63a5\uff0c\u53ef\u4ee5\u53c2\u770b\u5f80\u671f\u6587\u7ae0\uff1ahttps:\/\/www.madbull.site\/?p=939\uff0c\u5bf9\u4e8e\u8d44\u6e90\u7684\u7533\u8bf7\u91ca\u653e\u6bd4\u8f83\u65b9\u4fbf\uff0c\u4e5f\u4e0d\u7528\u8003\u8651\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u6b64\u6b21\u793a\u4f8b\u5982\u679c\u7528\u5728\u5e76\u53d1\u4e2d\u4f7f\u7528\uff0ccurl\u53e5\u67c4\u662f\u4e34\u754c\u8d44\u6e90\uff0c\u9700\u8981\u505a\u597d\u4e92\u65a5\u8bbf\u95ee\uff0c\u6216\u8005\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u7533\u8bf7\u4e0d\u540c\u7684curl\u53e5\u67c4\uff0c\u4e5f\u53ef\u4ee5\uff0c\u9700\u8981\u7075\u6d3b\u5904\u7406\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":[406,299,377,300,410,408,411,409,407],"class_list":["post-998","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-client","tag-http","tag-libcurl","tag-post","tag-tcpdump","tag-408","tag-411","tag-409","tag-407"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/998","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=998"}],"version-history":[{"count":8,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/998\/revisions"}],"predecessor-version":[{"id":1589,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/998\/revisions\/1589"}],"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=998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}