{"id":9290,"date":"2025-04-18T17:19:12","date_gmt":"2025-04-18T09:19:12","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/9290.html"},"modified":"2025-04-18T17:19:12","modified_gmt":"2025-04-18T09:19:12","slug":"linux58http-%e8%87%aa%e5%b7%b1%e6%9e%84%e5%bb%ba%e6%9c%8d%e5%8a%a1%e5%99%a8-%e5%ae%9e%e7%8e%b0%e7%bd%91%e9%a1%b5%e5%88%86%e7%a6%bb-%e8%ae%be%e8%ae%a1%e6%80%9d%e8%b7%af","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/9290.html","title":{"rendered":"[Linux#58][HTTP] \u81ea\u5df1\u6784\u5efa\u670d\u52a1\u5668 | \u5b9e\u73b0\u7f51\u9875\u5206\u79bb | \u8bbe\u8ba1\u601d\u8def"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"yGLU1-toc\" style=\"margin-left:0px\">\u4e00. \u6700\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668<\/p>\n<p id=\"WDYDy-toc\" style=\"margin-left:0px\">\u4e8c.\u670d\u52a1\u5668 2.0<\/p>\n<p id=\"jYTBI-toc\" style=\"margin-left:40px\">Protocol.hpp<\/p>\n<p id=\"i04sP-toc\" style=\"margin-left:40px\">httpServer.hpp<\/p>\n<p id=\"WgOPv-toc\" style=\"margin-left:80px\">\u5b50\u8fdb\u7a0b\u7684\u521b\u5efa\u548c\u9000\u51fa<\/p>\n<p id=\"EHBiW-toc\" style=\"margin-left:80px\">\u5b50\u8fdb\u7a0b\u9000\u51fa\u7684\u610f\u4e49<\/p>\n<p id=\"fCxMX-toc\" style=\"margin-left:80px\">\u7236\u8fdb\u7a0b\u5173\u95ed\u8fde\u63a5\u5957\u63a5\u5b57<\/p>\n<p id=\"U5Ygc-toc\" style=\"margin-left:40px\">httpServer.cc<\/p>\n<p id=\"rmsxE-toc\" style=\"margin-left:80px\">argc (argument count)<\/p>\n<p id=\"ind2k-toc\" style=\"margin-left:80px\">argv (argument vector)<\/p>\n<p id=\"lGxJd-toc\" style=\"margin-left:0px\">\u4e09.\u670d\u52a1\u5668\u548c\u7f51\u9875\u5206\u79bb<\/p>\n<p id=\"wWHZh-toc\" style=\"margin-left:40px\">\u601d\u8003\u4e0e\u8865\u5145&#xff1a;<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 style=\"background-color:transparent\">\u4e00. \u6700\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668<\/h2>\n<p>\u57fa\u4e8e\u4e0a\u4e00\u7bc7\u6587\u7ae0\u7684\u7406\u8bba&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1200\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091904-68021908f3a80.png\" width=\"1200\" \/><\/p>\n<p id=\"ud13def23\">\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684 HTTP \u670d\u52a1\u5668&#xff0c;\u5b83\u53ef\u4ee5\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5\u5e76\u8fd4\u56de\u4e00\u4e2a &#034;Hello World&#034; \u7f51\u9875\u3002\u4e3a\u4e86\u8be6\u7ec6\u8bf4\u660e\u8fd9\u6bb5\u4ee3\u7801&#xff0c;\u8ba9\u6211\u4eec\u9010\u884c\u8fdb\u884c\u89e3\u91ca\u3002<\/p>\n<p>#include &lt;sys\/socket.h&gt;  \/\/ \u5f15\u5165\u5957\u63a5\u5b57\u76f8\u5173\u7684\u5934\u6587\u4ef6<br \/>\n#include &lt;netinet\/in.h&gt;  \/\/ \u5f15\u5165\u5904\u7406IPv4\u5730\u5740\u7684\u5934\u6587\u4ef6<br \/>\n#include &lt;arpa\/inet.h&gt;   \/\/ \u5f15\u5165INET\u76f8\u5173\u51fd\u6570\u7684\u5934\u6587\u4ef6<br \/>\n#include &lt;unistd.h&gt;      \/\/ \u5f15\u5165UNIX\u6807\u51c6\u51fd\u6570&#xff0c;\u5982close()<br \/>\n#include &lt;stdio.h&gt;       \/\/ \u5f15\u5165\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u5934\u6587\u4ef6<br \/>\n#include &lt;string.h&gt;      \/\/ \u5f15\u5165\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u7684\u5934\u6587\u4ef6<br \/>\n#include &lt;stdlib.h&gt;      \/\/ \u5f15\u5165\u6807\u51c6\u5e93\u51fd\u6570&#xff0c;\u5982atoi() <\/p>\n<p id=\"uea29bcea\">\u8fd9\u4e9b\u5934\u6587\u4ef6\u5305\u542b\u4e86\u7a0b\u5e8f\u6240\u9700\u7684\u5404\u79cd\u51fd\u6570\u548c\u7c7b\u578b&#xff1a;<\/p>\n<ul>\n<li id=\"u79355aa1\">sys\/socket.h: \u63d0\u4f9b\u5957\u63a5\u5b57\u51fd\u6570\u548c\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<li id=\"ua1ee49c7\">netinet\/in.h: \u63d0\u4f9b\u4e86\u7528\u4e8e\u5904\u7406 IPv4 \u5730\u5740\u7684\u7ed3\u6784\u548c\u51fd\u6570\u3002<\/li>\n<li id=\"ubf24be91\">arpa\/inet.h: \u63d0\u4f9b\u4e86\u7528\u4e8e\u64cd\u4f5c IP \u5730\u5740\u7684\u51fd\u6570&#xff0c;\u5982 inet_addr\u3002<\/li>\n<li id=\"uc60f9f4d\">unistd.h: \u63d0\u4f9b\u4e86 UNIX \u6807\u51c6\u51fd\u6570&#xff0c;\u5982 close\u3002<\/li>\n<li id=\"uf3865d60\">stdlib.h: \u63d0\u4f9b\u4e86\u4e00\u4e9b\u6807\u51c6\u5e93\u51fd\u6570&#xff0c;\u5982 atoi\u3002<\/li>\n<\/ul>\n<p>void Usage() {<br \/>\n    printf(&#034;usage: .\/server [ip] [port]\\\\n&#034;);<br \/>\n} <\/p>\n<p id=\"u83faab7c\">\u5b9a\u4e49\u4e86\u4e00\u4e2a Usage \u51fd\u6570&#xff0c;<span style=\"color:#4da8ee\">\u8be5\u51fd\u6570\u6253\u5370\u4f7f\u7528\u8bf4\u660e<\/span>&#xff0c;\u8bf4\u660e\u7a0b\u5e8f\u9700\u8981\u4e24\u4e2a\u547d\u4ee4\u884c\u53c2\u6570&#xff0c;\u5373 IP \u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002<\/p>\n<p>int main(int argc, char* argv[]) { <\/p>\n<p id=\"u8bbb97df\">\u7a0b\u5e8f\u7684 main \u51fd\u6570\u5f00\u59cb\u3002<\/p>\n<p>    if (argc !&#061; 3) {<br \/>\n        Usage();<br \/>\n        return 1;<br \/>\n    } <\/p>\n<p id=\"u636fbe07\">\u68c0\u67e5\u547d\u4ee4\u884c\u53c2\u6570\u7684\u6570\u91cf\u3002\u5982\u679c\u53c2\u6570\u6570\u91cf\u4e0d\u7b49\u4e8e <span style=\"color:#4da8ee\">3&#xff08;\u7a0b\u5e8f\u540d\u3001IP \u5730\u5740\u548c\u7aef\u53e3\u53f7&#xff09;<\/span><\/p>\n<p>    int fd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (fd &lt; 0) {<br \/>\n        perror(&#034;socket&#034;); \/\/ \u5982\u679c\u521b\u5efa\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n        return 1;<br \/>\n    } <\/p>\n<p id=\"u3f3f6b2d\"><span style=\"color:#4da8ee\">\u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57<\/span>\u3002AF_INET \u8868\u793a\u4f7f\u7528 IPv4&#xff0c;SOCK_STREAM \u8868\u793a\u4f7f\u7528 TCP\u3002<\/p>\n<p>    struct sockaddr_in addr; \/\/ \u5b9a\u4e49\u4e00\u4e2a\u5730\u5740\u7ed3\u6784\u4f53<br \/>\n    addr.sin_family &#061; AF_INET; \/\/ \u8bbe\u7f6e\u4e3aIPv4\u5730\u5740\u65cf<br \/>\n    addr.sin_addr.s_addr &#061; inet_addr(argv[1]); \/\/ \u8bbe\u7f6eIP\u5730\u5740<br \/>\n    addr.sin_port &#061; htons(atoi(argv[2])); \/\/ \u8bbe\u7f6e\u7aef\u53e3\u53f7&#xff0c;\u5e76\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f <\/p>\n<p id=\"uf88d3d42\">\u8bbe\u7f6e\u670d\u52a1\u5668\u7aef\u5730\u5740&#xff1a;<\/p>\n<ul>\n<li id=\"u9ac1e74a\">sin_family&#xff1a;\u5bb6\u65cf\u7c7b\u578b\u4e3a AF_INET&#xff0c;\u5373 IPv4\u3002<\/li>\n<li id=\"u63f9c88e\">sin_addr.s_addr&#xff1a;\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u7684 IP \u5730\u5740\u8f6c\u5316\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u4e8c\u8fdb\u5236\u5730\u5740\u3002<\/li>\n<li id=\"ue21c4429\">sin_port&#xff1a;\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u7684\u7aef\u53e3\u53f7\u8f6c\u5316\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u7aef\u53e3\u53f7\u3002<\/li>\n<\/ul>\n<p>    int ret &#061; bind(fd, (struct sockaddr*)&amp;addr, sizeof(addr));<br \/>\n    if (ret &lt; 0) {<br \/>\n        perror(&#034;bind&#034;); \/\/ \u5982\u679c\u7ed1\u5b9a\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n        return 1;<br \/>\n    } <\/p>\n<p id=\"u745a805e\">\u5c06<span style=\"color:#4da8ee\">\u5957\u63a5\u5b57\u7ed1\u5b9a\u5230\u6307\u5b9a\u7684 IP \u5730\u5740\u548c\u7aef\u53e3\u3002<\/span><\/p>\n<p>    ret &#061; listen(fd, 10);<br \/>\n    if (ret &lt; 0) {<br \/>\n        perror(&#034;listen&#034;); \/\/ \u5982\u679c\u76d1\u542c\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n        return 1;<br \/>\n    } <\/p>\n<p id=\"u26b6122f\">\u5f00\u59cb\u76d1\u542c\u8fde\u63a5&#xff0c;\u5141\u8bb8\u6700\u591a 10 \u4e2a\u8fde\u63a5\u7b49\u5f85\u961f\u5217\u3002<\/p>\n<p>    for (;;) {<br \/>\n        struct sockaddr_in client_addr; \/\/ \u5b9a\u4e49\u5ba2\u6237\u7aef\u5730\u5740\u7ed3\u6784\u4f53<br \/>\n        socklen_t len &#061; sizeof(client_addr); \/\/ \u5b9a\u4e49\u957f\u5ea6\u53d8\u91cf<br \/>\n        int client_fd &#061; accept(fd, (struct sockaddr*)&amp;client_addr, &amp;len);<br \/>\n        if (client_fd &lt; 0) {<br \/>\n            perror(&#034;accept&#034;); \/\/ \u5982\u679c\u63a5\u53d7\u8fde\u63a5\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n            continue; \/\/ \u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af<br \/>\n        } <\/p>\n<p id=\"ua8e6632c\">\u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&#xff0c;\u6301\u7eed\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5&#xff1a;<\/p>\n<ul>\n<li id=\"u14e0e225\"><span style=\"color:#4da8ee\">client_addr&#xff1a;\u7528\u4e8e\u5b58\u50a8\u5ba2\u6237\u7aef\u7684\u5730\u5740\u3002<\/span><\/li>\n<li id=\"u7661cf60\">len&#xff1a;\u4fdd\u5b58\u5730\u5740 client_addr \u7684\u957f\u5ea6\u3002<\/li>\n<li id=\"uab67f343\">accept&#xff1a;\u63a5\u53d7\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<p id=\"ufba95fbc\">\u5982\u679c accept \u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f\u5e76\u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/p>\n<p>        char input_buf[1024 * 10] &#061; {0};<br \/>\n        ssize_t read_size &#061; read(client_fd, input_buf, sizeof(input_buf) &#8211; 1);<br \/>\n        if (read_size &lt; 0) {<br \/>\n            perror(&#034;read&#034;); \/\/ \u5982\u679c\u8bfb\u53d6\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n            close(client_fd); \/\/ \u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<br \/>\n            continue; \/\/ \u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af<br \/>\n        }<\/p>\n<p>        printf(&#034;[Request] %s\\\\n&#034;, input_buf); <\/p>\n<p id=\"ub82833ab\">\u5b9a\u4e49\u4e00\u4e2a\u7f13\u51b2\u533a<span style=\"color:#4da8ee\">\u5e76\u8bfb\u53d6\u5ba2\u6237\u7aef\u6570\u636e&#xff1a;<\/span><\/p>\n<ul>\n<li id=\"u26ba53af\">input_buf&#xff1a;\u5b58\u50a8\u4ece\u5ba2\u6237\u7aef\u8bfb\u53d6\u7684\u6570\u636e\u3002<\/li>\n<li id=\"uc4d9f244\">read&#xff1a;\u4ece\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u8bfb\u53d6\u6570\u636e\u81f3\u7f13\u51b2\u533a\u3002<\/li>\n<\/ul>\n<p id=\"ubf58483c\">\u5982\u679c\u8bfb\u53d6\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f&#xff0c;\u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57&#xff0c;\u5e76\u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/p>\n<p>        char buf[1024] &#061; {0};<br \/>\n        const char* hello &#061; &#034;&lt;h1&gt;hello world&lt;\/h1&gt;&#034;;<br \/>\n        sprintf(buf, &#034;HTTP\/1.0 200 OK\\\\nContent-Length:%lu\\\\n\\\\n%s&#034;, strlen(hello), hello);<br \/>\n        write(client_fd, buf, strlen(buf)); <\/p>\n<p id=\"u5cceb59d\">\u5b9a\u4e49\u4e00\u4e2a\u7f13\u51b2\u533a\u5e76\u53d1\u9001\u54cd\u5e94&#xff1a;<\/p>\n<ul>\n<li id=\"u54b3e7f3\">hello&#xff1a;\u8981\u53d1\u9001\u7684 HTML \u5185\u5bb9\u3002<\/li>\n<li id=\"u02e26b5f\">sprintf&#xff1a;\u683c\u5f0f\u5316 HTTP \u54cd\u5e94&#xff0c;\u5305\u62ec\u5934\u90e8\u548c\u5185\u5bb9\u3002<\/li>\n<li id=\"ufdb14a46\"><span style=\"color:#4da8ee\">write&#xff1a;\u5c06\u54cd\u5e94\u53d1\u9001\u56de\u5ba2\u6237\u7aef\u3002<\/span><\/li>\n<\/ul>\n<p>        close(client_fd); \/\/ \u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<br \/>\n    } <\/p>\n<p id=\"uda9675b8\">\u5173\u95ed\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/p>\n<p>    close(fd); \/\/ \u5173\u95ed\u670d\u52a1\u5668\u5957\u63a5\u5b57<br \/>\n    return 0; \/\/ \u6b63\u5e38\u9000\u51fa<br \/>\n} <\/p>\n<p id=\"u596be47f\">\u5173\u95ed\u670d\u52a1\u5668\u5957\u63a5\u5b57\u5e76\u6b63\u5e38\u9000\u51fa\u7a0b\u5e8f\u3002<\/p>\n<p id=\"u8b747bd3\">\u603b\u7ed3&#xff1a;<\/p>\n<p id=\"u92c140ea\">\u8be5\u7a0b\u5e8f\u662f\u4e00\u4e2a\u57fa\u672c\u7684 HTTP \u670d\u52a1\u5668&#xff0c;\u8d1f\u8d23\u76d1\u542c\u6307\u5b9a\u7684 IP \u5730\u5740\u548c\u7aef\u53e3&#xff0c;\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u8bfb\u53d6\u8bf7\u6c42\u5e76\u53d1\u9001\u4e00\u4e2a\u5305\u542b &#034;Hello World&#034; \u7684 HTML \u54cd\u5e94\u3002\u5b83<span style=\"color:#4da8ee\">\u901a\u8fc7\u4f7f\u7528 UNIX \u7cfb\u7edf\u8c03\u7528&#xff08;\u5982 socket\u3001bind\u3001listen \u548c accept \u7b49&#xff09;\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/span><\/p>\n<p id=\"u4377a77b\">\u5b8c\u6574\u4ee3\u7801&#xff1a;<\/p>\n<p>#include &lt;sys\/socket.h&gt;  \/\/ \u5f15\u5165\u5957\u63a5\u5b57\u76f8\u5173\u7684\u5934\u6587\u4ef6<br \/>\n#include &lt;netinet\/in.h&gt;    \/\/ \u5f15\u5165\u5904\u7406IPv4\u5730\u5740\u7684\u5934\u6587\u4ef6<br \/>\n#include &lt;arpa\/inet.h&gt;     \/\/ \u5f15\u5165INET\u76f8\u5173\u51fd\u6570\u7684\u5934\u6587\u4ef6<br \/>\n#include &lt;unistd.h&gt;        \/\/ \u5f15\u5165UNIX\u6807\u51c6\u51fd\u6570&#xff0c;\u5982close()<br \/>\n#include &lt;stdio.h&gt;         \/\/ \u5f15\u5165\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u5934\u6587\u4ef6<br \/>\n#include &lt;string.h&gt;        \/\/ \u5f15\u5165\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u7684\u5934\u6587\u4ef6<br \/>\n#include &lt;stdlib.h&gt;        \/\/ \u5f15\u5165\u6807\u51c6\u5e93\u51fd\u6570&#xff0c;\u5982atoi()<\/p>\n<p>\/\/ \u6253\u5370\u670d\u52a1\u5668\u7684\u4f7f\u7528\u65b9\u6cd5<br \/>\nvoid Usage() {<br \/>\n    printf(&#034;usage: .\/server [ip] [port]\\\\n&#034;);<br \/>\n}<\/p>\n<p>int main(int argc, char* argv[]) {<br \/>\n    \/\/ \u786e\u4fdd\u547d\u4ee4\u884c\u53c2\u6570\u6570\u91cf\u6b63\u786e&#xff08;\u5e94\u4e3a3\u4e2a&#xff1a;\u7a0b\u5e8f\u540d\u3001IP\u5730\u5740\u548c\u7aef\u53e3\u53f7&#xff09;<br \/>\n    if (argc !&#061; 3) {<br \/>\n        Usage();<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    \/\/ \u521b\u5efa\u4e00\u4e2a\u57fa\u4e8eIPv4\u7684TCP\u5957\u63a5\u5b57<br \/>\n    int fd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (fd &lt; 0) {<br \/>\n        perror(&#034;socket&#034;); \/\/ \u5982\u679c\u521b\u5efa\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    struct sockaddr_in addr; \/\/ \u5b9a\u4e49\u4e00\u4e2a\u5730\u5740\u7ed3\u6784\u4f53<br \/>\n    addr.sin_family &#061; AF_INET; \/\/ \u8bbe\u7f6e\u4e3aIPv4\u5730\u5740\u65cf<br \/>\n    addr.sin_addr.s_addr &#061; inet_addr(argv[1]); \/\/ \u8bbe\u7f6eIP\u5730\u5740<br \/>\n    addr.sin_port &#061; htons(atoi(argv[2])); \/\/ \u8bbe\u7f6e\u7aef\u53e3\u53f7&#xff0c;\u5e76\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f<\/p>\n<p>    \/\/ \u5c06\u5957\u63a5\u5b57\u7ed1\u5b9a\u5230\u6307\u5b9a\u7684IP\u5730\u5740\u548c\u7aef\u53e3<br \/>\n    int ret &#061; bind(fd, (struct sockaddr*)&amp;addr, sizeof(addr));<br \/>\n    if (ret &lt; 0) {<br \/>\n        perror(&#034;bind&#034;); \/\/ \u5982\u679c\u7ed1\u5b9a\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    \/\/ \u5f00\u59cb\u76d1\u542c\u4f20\u5165\u7684\u8fde\u63a5&#xff0c;\u5141\u8bb8\u6700\u591a10\u4e2a\u8fde\u63a5\u540c\u65f6\u7b49\u5f85<br \/>\n    ret &#061; listen(fd, 10);<br \/>\n    if (ret &lt; 0) {<br \/>\n        perror(&#034;listen&#034;); \/\/ \u5982\u679c\u76d1\u542c\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    \/\/ \u65e0\u9650\u5faa\u73af&#xff0c;\u6301\u7eed\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5<br \/>\n    for (;;) {<br \/>\n        struct sockaddr_in client_addr; \/\/ \u5b9a\u4e49\u5ba2\u6237\u7aef\u5730\u5740\u7ed3\u6784\u4f53<br \/>\n        socklen_t len &#061; sizeof(client_addr); \/\/ \u5b9a\u4e49\u957f\u5ea6\u53d8\u91cf<br \/>\n        \/\/ \u63a5\u53d7\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u5e76\u5c06\u5ba2\u6237\u7aef\u7684\u5730\u5740\u4fe1\u606f\u5b58\u50a8\u5728client_addr\u4e2d<br \/>\n        int client_fd &#061; accept(fd, (struct sockaddr*)&amp;client_addr, &amp;len);<br \/>\n        if (client_fd &lt; 0) {<br \/>\n            perror(&#034;accept&#034;); \/\/ \u5982\u679c\u63a5\u53d7\u8fde\u63a5\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n            continue; \/\/ \u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af<br \/>\n        }<\/p>\n<p>        \/\/ \u5b9a\u4e49\u4e00\u4e2a\u7f13\u51b2\u533a&#xff0c;\u7528\u4e8e\u5b58\u50a8\u4ece\u5ba2\u6237\u7aef\u8bfb\u53d6\u7684\u6570\u636e<br \/>\n        char input_buf[1024 * 10] &#061; {0};<br \/>\n        \/\/ \u4ece\u5ba2\u6237\u7aef\u8bfb\u53d6\u6570\u636e&#xff0c;\u6700\u591a\u8bfb\u53d6\u7f13\u51b2\u533a\u5927\u5c0f-1\u5b57\u8282<br \/>\n        ssize_t read_size &#061; read(client_fd, input_buf, sizeof(input_buf) &#8211; 1);<br \/>\n        if (read_size &lt; 0) {<br \/>\n            perror(&#034;read&#034;); \/\/ \u5982\u679c\u8bfb\u53d6\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n            close(client_fd); \/\/ \u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<br \/>\n            continue; \/\/ \u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af<br \/>\n        }<\/p>\n<p>        \/\/ \u6253\u5370\u63a5\u6536\u5230\u7684\u8bf7\u6c42<br \/>\n        printf(&#034;[Request] %s\\\\n&#034;, input_buf);<\/p>\n<p>        \/\/ \u5b9a\u4e49\u4e00\u4e2a\u7f13\u51b2\u533a&#xff0c;\u7528\u4e8e\u5b58\u50a8\u54cd\u5e94\u6570\u636e<br \/>\n        char buf[1024] &#061; {0};<br \/>\n        \/\/ \u5b9a\u4e49\u8981\u53d1\u9001\u7684HTML\u5185\u5bb9<br \/>\n        const char* hello &#061; &#034;&lt;h1&gt;hello world&lt;\/h1&gt;&#034;;<br \/>\n        \/\/ \u683c\u5f0f\u5316HTTP\u54cd\u5e94\u6d88\u606f&#xff0c;\u5305\u62ecHTTP\u5934\u90e8\u548cHTML\u5185\u5bb9<br \/>\n        sprintf(buf, &#034;HTTP\/1.0 200 OK\\\\nContent-Length:%lu\\\\n\\\\n%s&#034;, strlen(hello), hello);<br \/>\n        \/\/ \u5c06\u54cd\u5e94\u6d88\u606f\u53d1\u9001\u56de\u5ba2\u6237\u7aef<br \/>\n        write(client_fd, buf, strlen(buf));<\/p>\n<p>        \/\/ \u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<br \/>\n        close(client_fd);<br \/>\n    }<\/p>\n<p>    \/\/ \u5173\u95ed\u670d\u52a1\u5668\u5957\u63a5\u5b57<br \/>\n    close(fd);<\/p>\n<p>    return 0; \/\/ \u6b63\u5e38\u9000\u51fa<br \/>\n}<\/p>\n<h2 id=\"WDYDy\">\u4e8c.\u670d\u52a1\u5668 2.0<\/h2>\n<h3 id=\"jYTBI\">Protocol.hpp<\/h3>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>\/\/\u5ba2\u6237\u7aef<br \/>\nclass httpRequest<br \/>\n{<\/p>\n<p>public:<br \/>\n    httpRequest(){};<br \/>\n    ~httpRequest(){};<\/p>\n<p>public:<br \/>\n    string inbuffer;\/\/\u7f13\u51b2\u533a<\/p>\n<p> \/\/\u7b80\u5355\u4e00\u70b9\u4e3b\u8981\u770b\u4e00\u4e0bhttp\u7684\u7ec6\u8282<br \/>\n    \/\/ string reqline;\/\/\u8bf7\u6c42\u884c<br \/>\n    \/\/ vector&lt;std::string&gt; reqheader;\/\/\u62a5\u5934<br \/>\n    \/\/ string body;\/\/\u8bf7\u6c42\u6b63\u6587<\/p>\n<p>    \/\/\u7b2c\u4e00\u884c\u7ec6\u5206<br \/>\n    \/\/ string method;<br \/>\n    \/\/ string url;<br \/>\n    \/\/ string httpversion;<br \/>\n};<\/p>\n<p>\/\/\u670d\u52a1\u5668<br \/>\nclass httpResponse<br \/>\n{<\/p>\n<p>public:<br \/>\n    string outbuffer;\/\/\u7f13\u51b2\u533a<br \/>\n};<\/p>\n<h3 id=\"i04sP\">httpServer.hpp<\/h3>\n<p>#pragma once<br \/>\n\/\/ \u786e\u4fdd\u5934\u6587\u4ef6\u53ea\u88ab\u5305\u542b\u4e00\u6b21<\/p>\n<p>#include &#034;Protocol.hpp&#034;<br \/>\n\/\/ \u5305\u542b\u81ea\u5b9a\u4e49\u7684\u534f\u8bae\u5904\u7406\u5934\u6587\u4ef6&#xff0c;\u53ef\u80fd\u5b9a\u4e49\u4e86 httpRequest \u548c httpResponse \u7c7b<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;signal.h&gt;<br \/>\n#include &lt;functional&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>\/\/ \u5b9a\u4e49\u9519\u8bef\u7801\u679a\u4e3e<br \/>\nenum {<br \/>\n    USAGG_ERR &#061; 1, \/\/ \u4f7f\u7528\u9519\u8bef<br \/>\n    SOCKET_ERR,     \/\/ \u5957\u63a5\u5b57\u521b\u5efa\u9519\u8bef<br \/>\n    BIND_ERR,       \/\/ \u7ed1\u5b9a\u9519\u8bef<br \/>\n    LISTEN_ERR      \/\/ \u76d1\u542c\u9519\u8bef<br \/>\n};<\/p>\n<p>const int backlog &#061; 5;<br \/>\n\/\/ \u5b9a\u4e49\u76d1\u542c\u961f\u5217\u7684\u6700\u5927\u957f\u5ea6<\/p>\n<p>\/\/ \u5b9a\u4e49\u51fd\u6570\u7c7b\u578b\u522b\u540d&#xff0c;\u7528\u4e8e\u5904\u7406HTTP\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u56de\u8c03\u51fd\u6570<br \/>\ntypedef function&lt;void(const httpRequest&amp;, httpResponse&amp;)&gt; func_t;<\/p>\n<p>\/\/ \u5904\u7406HTTP\u8bf7\u6c42\u7684\u51fd\u6570<br \/>\nvoid handlerEntery(int sock,func_t callback)<br \/>\n{<br \/>\n    \/\/ 1. \u8bfb\u5230\u5b8c\u6574\u7684http\u8bf7\u6c42<br \/>\n    \/\/ 2. \u53cd\u5e8f\u5217\u5316<br \/>\n    \/\/ 3. httprequst, httpresponse, callback(req, resp)<br \/>\n    \/\/ 4. resp\u5e8f\u5217\u5316<br \/>\n    \/\/ 5. send<\/p>\n<p>char buffer[4096];<br \/>\n    httpRequest req;<br \/>\n    httpResponse resp;<br \/>\n    ssize_t n&#061;recv(sock,buffer,sizeof(buffer)-1,0);\/\/\u5927\u6982\u7387\u6211\u4eec\u76f4\u63a5\u5c31\u80fd\u8bfb\u53d6\u5230\u5b8c\u6574\u7684http\u8bf7\u6c42<br \/>\n    if(n&gt;0)<br \/>\n    {<br \/>\n        buffer[n]&#061;0;<br \/>\n        req.inbuffer&#061;buffer;<br \/>\n        callback(req,resp);<br \/>\n        send(sock,resp.outbuffer.c_str(),resp.outbuffer.size(),0);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ HTTP\u670d\u52a1\u5668\u7c7b<br \/>\nclass httpServer {<br \/>\npublic:<br \/>\n    \/\/ \u6784\u9020\u51fd\u6570&#xff0c;\u521d\u59cb\u5316\u7aef\u53e3\u53f7\u548c\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n    httpServer(const uint16_t port) : _port(port), _listensock(-1) {}<\/p>\n<p>    \/\/ \u521d\u59cb\u5316\u670d\u52a1\u5668<br \/>\n    void initServer() {<br \/>\n        \/\/ \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n        _listensock &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n        if (_listensock &lt; 0) {<br \/>\n            exit(SOCKET_ERR); \/\/ \u5982\u679c\u521b\u5efa\u5931\u8d25&#xff0c;\u9000\u51fa\u7a0b\u5e8f<br \/>\n        }<\/p>\n<p>        \/\/ \u7ed1\u5b9a\u5957\u63a5\u5b57\u5230\u672c\u5730\u5730\u5740\u548c\u7aef\u53e3<br \/>\n        struct sockaddr_in local;<br \/>\n        memset(&amp;local, 0, sizeof(local));<br \/>\n        local.sin_family &#061; AF_INET;<br \/>\n        local.sin_port &#061; htons(_port);<br \/>\n        local.sin_addr.s_addr &#061; INADDR_ANY; \/\/ \u7ed1\u5b9a\u5230\u4efb\u610f\u5730\u5740<\/p>\n<p>        if (bind(_listensock, (struct sockaddr *)&amp;local, sizeof(local)) &lt; 0) {<br \/>\n            exit(BIND_ERR); \/\/ \u5982\u679c\u7ed1\u5b9a\u5931\u8d25&#xff0c;\u9000\u51fa\u7a0b\u5e8f<br \/>\n        }<\/p>\n<p>        \/\/ \u8bbe\u7f6e\u5957\u63a5\u5b57\u4e3a\u76d1\u542c\u72b6\u6001<br \/>\n        if (listen(_listensock, backlog) &lt; 0) { \/\/ \u8bbe\u7f6e\u76d1\u542c\u961f\u5217\u957f\u5ea6<br \/>\n            exit(LISTEN_ERR); \/\/ \u5982\u679c\u76d1\u542c\u5931\u8d25&#xff0c;\u9000\u51fa\u7a0b\u5e8f<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u542f\u52a8\u670d\u52a1\u5668&#xff0c;\u5904\u7406\u8bf7\u6c42<br \/>\n    void start(func_t func) {<br \/>\n        \/\/ \u5ffd\u7565\u5b50\u8fdb\u7a0b\u7ed3\u675f\u7684\u4fe1\u53f7<br \/>\n        signal(SIGCHLD, SIG_IGN);<br \/>\n        for (;;) { \/\/ \u65e0\u9650\u5faa\u73af&#xff0c;\u7b49\u5f85\u8fde\u63a5<br \/>\n            \/\/ \u63a5\u53d7\u65b0\u8fde\u63a5<br \/>\n            struct sockaddr_in peer;<br \/>\n            socklen_t len &#061; sizeof(peer);<br \/>\n            int sock &#061; accept(_listensock, (struct sockaddr *)&amp;peer, &amp;len);<br \/>\n            if (sock &lt; 0) {<br \/>\n                continue; \/\/ \u5982\u679c\u63a5\u53d7\u5931\u8d25&#xff0c;\u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af<br \/>\n            }<\/p>\n<p>            \/\/ \u521b\u5efa\u5b50\u8fdb\u7a0b\u5904\u7406\u8fde\u63a5<br \/>\n            int fd &#061; fork();<br \/>\n            if (fd &#061;&#061; 0) {<br \/>\n                close(_listensock); \/\/ \u5b50\u8fdb\u7a0b\u5173\u95ed\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n                handlerEntery(sock, func); \/\/ \u5904\u7406HTTP\u8bf7\u6c42<br \/>\n                close(sock); \/\/ \u5904\u7406\u5b8c\u6bd5\u540e\u5173\u95ed\u8fde\u63a5\u5957\u63a5\u5b57<br \/>\n                exit(0); \/\/ \u5b50\u8fdb\u7a0b\u9000\u51fa<br \/>\n            }<br \/>\n            close(sock); \/\/ \u7236\u8fdb\u7a0b\u5173\u95ed\u8fde\u63a5\u5957\u63a5\u5b57<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u6790\u6784\u51fd\u6570<br \/>\n    ~httpServer() {<br \/>\n        \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u8fdb\u884c\u8d44\u6e90\u6e05\u7406<br \/>\n    }<\/p>\n<p>private:<br \/>\n    uint16_t _port; \/\/ \u670d\u52a1\u5668\u76d1\u542c\u7684\u7aef\u53e3\u53f7<br \/>\n    int _listensock; \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n};<\/p>\n<p id=\"u398c8459\">\u2b55 \u5982\u4f55\u7406\u89e3\u8fd9\u4e2a\u5730\u65b9\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u548c\u5173\u95ed?<\/p>\n<p id=\"u67016ea4\">\u5728 start \u51fd\u6570\u4e2d&#xff0c;\u670d\u52a1\u5668\u901a\u8fc7 fork() \u7cfb\u7edf\u8c03\u7528\u521b\u5efa\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u6bcf\u4e2a\u65b0\u7684\u8fde\u63a5\u3002\u4ee5\u4e0b\u662f\u5bf9\u5b50\u8fdb\u7a0b\u9000\u51fa\u548c\u5173\u95ed\u64cd\u4f5c\u7684\u8be6\u7ec6\u89e3\u91ca&#xff1a;<\/p>\n<h4 id=\"WgOPv\">\u5b50\u8fdb\u7a0b\u7684\u521b\u5efa\u548c\u9000\u51fa<\/h4>\n<li id=\"uaf79a4b9\">\u521b\u5efa\u5b50\u8fdb\u7a0b:<\/li>\n<ul>\n<li>\n<ul>\n<li id=\"uc820b563\">int fd &#061; fork();: \u8fd9\u884c\u4ee3\u7801\u662f\u521b\u5efa\u5b50\u8fdb\u7a0b\u7684\u5173\u952e\u3002fork() \u8c03\u7528\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b50\u8fdb\u7a0b\u3002\u5728\u7236\u8fdb\u7a0b\u4e2d&#xff0c;fork() \u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0bID&#xff1b;\u5728\u5b50\u8fdb\u7a0b\u4e2d&#xff0c;fork() \u8fd4\u56de0\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<li id=\"u9de79ba5\">\u5728\u5b50\u8fdb\u7a0b\u4e2d (if (fd &#061;&#061; 0)):<\/li>\n<ul>\n<li>\n<ul>\n<li id=\"ufed74d8f\">close(_listensock);: \u5b50\u8fdb\u7a0b\u4e0d\u9700\u8981\u76d1\u542c\u65b0\u7684\u8fde\u63a5&#xff0c;\u56e0\u6b64\u5b83\u5173\u95ed\u76d1\u542c\u5957\u63a5\u5b57\u3002\u8fd9\u662f\u56e0\u4e3a\u76d1\u542c\u5957\u63a5\u5b57\u7531\u7236\u8fdb\u7a0b\u8d1f\u8d23&#xff0c;\u5e76\u4e14\u6240\u6709\u5b50\u8fdb\u7a0b\u90fd\u4f1a\u7ee7\u627f\u7236\u8fdb\u7a0b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li id=\"uedb009f8\">handlerEntery(sock, func);: \u5b50\u8fdb\u7a0b\u8c03\u7528 handlerEntery \u51fd\u6570\u6765\u5904\u7406HTTP\u8bf7\u6c42\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8bfb\u53d6\u8bf7\u6c42\u3001\u53cd\u5e8f\u5217\u5316\u3001\u8c03\u7528\u56de\u8c03\u51fd\u6570\u5904\u7406\u8bf7\u6c42\u3001\u5e8f\u5217\u5316\u54cd\u5e94\u5e76\u53d1\u9001\u54cd\u5e94\u3002<\/li>\n<li id=\"u168fc00c\">close(sock);: \u5728\u5904\u7406\u5b8c\u8bf7\u6c42\u5e76\u53d1\u9001\u54cd\u5e94\u540e&#xff0c;\u5b50\u8fdb\u7a0b\u5173\u95ed\u4e0e\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u5957\u63a5\u5b57&#xff0c;\u56e0\u4e3a\u5b83\u4e0d\u518d\u9700\u8981\u8fd9\u4e2a\u5957\u63a5\u5b57\u3002<\/li>\n<li id=\"u79b92db9\">exit(0);: \u5b50\u8fdb\u7a0b\u901a\u8fc7 exit(0) \u9000\u51fa\u3002\u8fd9\u4e2a\u8c03\u7528\u4f1a\u5bfc\u81f4\u5b50\u8fdb\u7a0b\u7ec8\u6b62&#xff0c;\u5e76\u4e14\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u56de\u6536\u5b50\u8fdb\u7a0b\u5360\u7528\u7684\u6240\u6709\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4 id=\"EHBiW\">\u5b50\u8fdb\u7a0b\u9000\u51fa\u7684\u610f\u4e49<\/h4>\n<ul>\n<li id=\"u64f52487\">\u8d44\u6e90\u56de\u6536: \u5f53\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u56de\u6536\u5b50\u8fdb\u7a0b\u6240\u5360\u7528\u7684\u6240\u6709\u8d44\u6e90&#xff0c;\u5305\u62ec\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u5185\u5b58\u7b49\u3002\u8fd9\u662f\u975e\u5e38\u91cd\u8981\u7684&#xff0c;\u56e0\u4e3a\u5982\u679c\u4e0d\u56de\u6536\u8d44\u6e90&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<li id=\"u36814266\">\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b: \u5728\u8c03\u7528 fork() \u4e4b\u524d&#xff0c;\u7236\u8fdb\u7a0b\u901a\u8fc7 signal(SIGCHLD, SIG_IGN); \u5ffd\u7565\u4e86 SIGCHLD \u4fe1\u53f7\u3002\u8fd9\u610f\u5473\u7740\u5f53\u5b50\u8fdb\u7a0b\u7ed3\u675f\u65f6&#xff0c;\u7236\u8fdb\u7a0b\u4e0d\u4f1a\u6536\u5230\u901a\u77e5&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u6e05\u7406\u6389\u5b50\u8fdb\u7a0b&#xff0c;\u9632\u6b62\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<h4 id=\"fCxMX\">\u7236\u8fdb\u7a0b\u5173\u95ed\u8fde\u63a5\u5957\u63a5\u5b57<\/h4>\n<ul>\n<li id=\"u5674335e\">close(sock);: \u5728\u7236\u8fdb\u7a0b\u4e2d&#xff0c;fork() \u8fd4\u56de\u7684\u662f\u5b50\u8fdb\u7a0b\u7684ID&#xff0c;\u56e0\u6b64\u7236\u8fdb\u7a0b\u4e0d\u4f1a\u8fdb\u5165 if (fd &#061;&#061; 0) \u5757\u3002\u7236\u8fdb\u7a0b\u4e5f\u4e0d\u9700\u8981\u8fd9\u4e2a\u4e0e\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u5957\u63a5\u5b57&#xff0c;\u56e0\u4e3a\u5b83\u53ea\u8d1f\u8d23\u76d1\u542c\u65b0\u7684\u8fde\u63a5&#xff0c;\u6240\u4ee5\u5b83\u5173\u95ed\u8fd9\u4e2a\u5957\u63a5\u5b57\u3002 \u603b\u7ed3\u6765\u8bf4&#xff0c;\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u548c\u5173\u95ed\u64cd\u4f5c\u786e\u4fdd\u4e86\u6bcf\u4e2aHTTP\u8bf7\u6c42\u90fd\u80fd\u88ab\u5355\u72ec\u7684\u5b50\u8fdb\u7a0b\u5904\u7406&#xff0c;\u5e76\u4e14\u5728\u5904\u7406\u5b8c\u6210\u540e&#xff0c;\u5b50\u8fdb\u7a0b\u80fd\u591f\u5e72\u51c0\u5730\u9000\u51fa&#xff0c;\u4e0d\u4f1a\u7559\u4e0b\u50f5\u5c38\u8fdb\u7a0b\u6216\u8d44\u6e90\u6cc4\u6f0f\u3002\u7236\u8fdb\u7a0b\u7ee7\u7eed\u76d1\u542c\u65b0\u7684\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u800c\u5b50\u8fdb\u7a0b\u5219\u8d1f\u8d23\u5904\u7406\u5df2\u7ecf\u63a5\u53d7\u7684\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<hr id=\"cNgf2\" \/>\n<h3 id=\"U5Ygc\">httpServer.cc<\/h3>\n<p>#include &#034;httpServer.hpp&#034;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>\/\/ \u6253\u5370\u7a0b\u5e8f\u4f7f\u7528\u65b9\u6cd5\u7684\u51fd\u6570<br \/>\nvoid Usage(const string&amp; proc) {<br \/>\n    cout &lt;&lt; &#034;\\\\nUsage:\\\\n\\\\t&#034; &lt;&lt; proc &lt;&lt; &#034; local_port\\\\n\\\\n&#034;;<br \/>\n}<\/p>\n<p>\/\/ \u5904\u7406HTTP GET\u8bf7\u6c42\u7684\u51fd\u6570&#xff0c;\u53c2\u6570\u4e3a\u8bf7\u6c42\u548c\u54cd\u5e94\u5bf9\u8c61<br \/>\nvoid Get(const httpRequest &amp;req, httpResponse &amp;resp)<br \/>\n{<\/p>\n<p>    cout &lt;&lt; &#034;&#8212;&#8212;&#8212;&#8212;&#8212;-http start&#8212;&#8212;&#8212;&#8212;&#8212;&#034; &lt;&lt; endl;<br \/>\n    cout &lt;&lt; req.inbuffer &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;&#8212;&#8212;&#8212;&#8212;&#8212;-http end&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#034; &lt;&lt; endl;<\/p>\n<p>     string respline &#061; &#034;HTTP\/1.1 200 OK\\\\r\\\\n&#034;;<br \/>\n    \/\/ string respheader;<br \/>\n     string respblank &#061; &#034;\\\\r\\\\n&#034;;<\/p>\n<p>\/\/\u968f\u4fbf\u505a\u4e00\u4e2a\u7f51\u9875<br \/>\n     string body&#061;&#034;&lt;html lang&#061;\\\\&#034;en\\\\&#034;&gt;&lt;head&gt;&lt;meta charset&#061;\\\\&#034;UTF-8\\\\&#034;&gt;&lt;title&gt;for test&lt;\/title&gt;&lt;h1&gt;hello world&lt;\/h1&gt;&lt;\/head&gt;&lt;body&gt;&lt;p&gt;\u4f60\u597d\u5440 \u795d\u4f60\u5929\u5929\u5f00\u5fc3~&lt;\/p&gt;&lt;\/body&gt;&lt;\/html&gt;&#034;;<\/p>\n<p>\/\/\u5e8f\u5217\u5316<br \/>\n     resp.outbuffer &#043;&#061; respline;<br \/>\n     resp.outbuffer &#043;&#061; respblank;<br \/>\n     resp.outbuffer &#043;&#061; body;<br \/>\n}<\/p>\n<p>\/\/ \u7a0b\u5e8f\u5165\u53e3\u70b9<br \/>\nint main(int argc, char* argv[]) {<br \/>\n    \/\/ \u68c0\u67e5\u547d\u4ee4\u884c\u53c2\u6570\u6570 \u91cf\u662f\u5426\u6b63\u786e<br \/>\n    if (argc !&#061; 2) {<br \/>\n        \/\/ \u5982\u679c\u53c2\u6570\u6570\u91cf\u4e0d\u6b63\u786e&#xff0c;\u663e\u793a\u4f7f\u7528\u65b9\u6cd5\u5e76\u9000\u51fa<br \/>\n        Usage(argv[0]);<br \/>\n        exit(USAGG_ERR); \/\/ \u5047\u8bbe USAGG_ERR \u662f\u4e00\u4e2a\u5b9a\u4e49\u7684\u9519\u8bef\u4ee3\u7801<br \/>\n    }<\/p>\n<p>    \/\/ \u5c06\u547d\u4ee4\u884c\u53c2\u6570\u8f6c\u6362\u4e3a\u7aef\u53e3\u53f7<br \/>\n    uint16_t serverport &#061; static_cast&lt;uint16_t&gt;(atoi(argv[1]));<\/p>\n<p>    \/\/ \u4f7f\u7528\u667a\u80fd\u6307\u9488\u521b\u5efahttpServer\u5b9e\u4f8b&#xff0c;\u81ea\u52a8\u7ba1\u7406\u5185\u5b58<br \/>\n    std::unique_ptr&lt;httpServer&gt; server(new httpServer(serverport));<br \/>\n    \/\/ \u521d\u59cb\u5316\u670d\u52a1\u5668<br \/>\n    server-&gt;initServer();<br \/>\n    \/\/ \u542f\u52a8\u670d\u52a1\u5668&#xff0c;\u5e76\u4f20\u5165Get\u51fd\u6570\u4f5c\u4e3a\u5904\u7406HTTP\u8bf7\u6c42\u7684\u56de\u8c03<br \/>\n    server-&gt;start(Get);<\/p>\n<p>    \/\/ \u7a0b\u5e8f\u6b63\u5e38\u7ed3\u675f<br \/>\n    return 0;<br \/>\n} <\/p>\n<p id=\"u309b0d8d\">\u2b55 \u89e3\u91caargc \u548cargv\u7684\u8bbe\u8ba1\u4e0e\u8fd0\u7528<\/p>\n<p id=\"ue05cabbe\">\u5728C\u548cC&#043;&#043;\u7a0b\u5e8f\u4e2d&#xff0c;argc \u548c argv \u662f main \u51fd\u6570\u7684\u4e24\u4e2a\u53c2\u6570&#xff0c;\u5b83\u4eec\u7528\u4e8e\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<h4 id=\"rmsxE\">argc (argument count)<\/h4>\n<p id=\"u8435921a\">argc \u662f\u4e00\u4e2a\u6574\u6570&#xff0c;\u4ee3\u8868\u4f20\u9012\u7ed9\u7a0b\u5e8f\u7684<span style=\"color:#4da8ee\">\u547d\u4ee4\u884c\u53c2\u6570\u7684\u6570\u91cf\u3002\u5b83\u81f3\u5c11\u603b\u662f\u4e3a1&#xff0c;\u56e0\u4e3a argv[0] \u603b\u662f\u5305\u542b\u7a0b\u5e8f\u7684\u540d\u79f0\u6216\u8def\u5f84\u3002<\/span><\/p>\n<h4 id=\"ind2k\">argv (argument vector)<\/h4>\n<p id=\"u9aeba80f\">argv \u662f\u4e00\u4e2a\u6307\u5411\u5b57\u7b26\u6307\u9488\u7684\u6307\u9488&#xff0c;\u5b83\u6307\u5411\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4&#xff0c;\u8fd9\u4e9b\u5b57\u7b26\u4e32\u5305\u542b\u4e86\u7a0b\u5e8f\u7684\u547d\u4ee4\u884c\u53c2\u6570\u3002argv[0] \u662f\u7a0b\u5e8f\u7684\u540d\u79f0\u6216\u8def\u5f84&#xff0c;argv[1] \u662f\u7b2c\u4e00\u4e2a\u547d\u4ee4\u884c\u53c2\u6570&#xff0c;\u4f9d\u6b64\u7c7b\u63a8\u3002 \u4ee5\u4e0b\u662f argc \u548c argv \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684\u8bbe\u8ba1\u4e0e\u8fd0\u7528&#xff1a;<\/p>\n<li id=\"uc4fe3a3d\">\u7a0b\u5e8f\u5165\u53e3\u70b9:<\/li>\n<p>int main(int argc, char* argv[]) { <\/p>\n<p id=\"ub9c79cd0\">\u8fd9\u91cc main \u51fd\u6570\u63a5\u6536 argc \u548c argv \u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<li id=\"u58f6f2a8\">\u68c0\u67e5\u53c2\u6570\u6570\u91cf:<\/li>\n<p>if (argc !&#061; 2) { <\/p>\n<p id=\"ue63eba0e\">\u7a0b\u5e8f\u671f\u671b\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u547d\u4ee4\u884c\u53c2\u6570&#xff0c;\u5373\u7aef\u53e3\u53f7\u3002\u5982\u679c argc \u4e0d\u7b49\u4e8e2&#xff08;\u7a0b\u5e8f\u540d\u79f0\u548c\u4e00\u4e2a\u53c2\u6570&#xff09;&#xff0c;\u5219\u8bf4\u660e\u7528\u6237\u6ca1\u6709\u6b63\u786e\u8f93\u5165\u53c2\u6570\u3002<\/p>\n<li id=\"uad9504e0\">\u6253\u5370\u4f7f\u7528\u65b9\u6cd5:<\/li>\n<p>Usage(argv[0]); <\/p>\n<p id=\"u057584ca\">\u5982\u679c\u53c2\u6570\u6570\u91cf\u4e0d\u6b63\u786e&#xff0c;\u7a0b\u5e8f\u8c03\u7528 Usage \u51fd\u6570&#xff0c;\u5e76\u4f20\u9012 argv[0] \u4f5c\u4e3a\u53c2\u6570&#xff0c;\u8fd9\u901a\u5e38\u662f\u7a0b\u5e8f\u7684\u540d\u79f0\u3002Usage \u51fd\u6570\u4f1a\u6253\u5370\u51fa\u5982\u4f55\u6b63\u786e\u4f7f\u7528\u7a0b\u5e8f\u7684\u4fe1\u606f\u3002<\/p>\n<li id=\"u0de75af7\">\u83b7\u53d6\u7aef\u53e3\u53f7:<\/li>\n<p>uint16_t serverport &#061; static_cast&lt;uint16_t&gt;(atoi(argv[1])); <\/p>\n<p id=\"u5aa9c13c\">\u7a0b\u5e8f\u5c06 argv[1]&#xff08;\u7b2c\u4e00\u4e2a\u547d\u4ee4\u884c\u53c2\u6570&#xff0c;\u5373\u7528\u6237\u8f93\u5165\u7684\u7aef\u53e3\u53f7\u5b57\u7b26\u4e32&#xff09;\u8f6c\u6362\u4e3a\u6574\u6570&#xff0c;\u5e76\u5c06\u5176\u5b58\u50a8\u5728 serverport \u53d8\u91cf\u4e2d\u3002<\/p>\n<li id=\"u9c238b8e\">\u542f\u52a8\u670d\u52a1\u5668: \u7a0b\u5e8f\u4f7f\u7528 serverport \u6765\u521d\u59cb\u5316\u548c\u542f\u52a8 httpServer \u5b9e\u4f8b\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f&#xff0c;<span style=\"color:#4da8ee\">argc \u548c argv \u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u4ece\u547d\u4ee4\u884c\u63a5\u6536\u7528\u6237\u8f93\u5165<\/span>&#xff0c;\u4f7f\u5f97\u7a0b\u5e8f\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u8f93\u5165\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d&#xff0c;\u5b83\u4eec\u7528\u4e8e\u6307\u5b9aHTTP\u670d\u52a1\u5668\u76d1\u542c\u7684\u7aef\u53e3\u53f7\u3002\u5982\u679c\u7528\u6237\u6ca1\u6709\u63d0\u4f9b\u6b63\u786e\u7684\u53c2\u6570&#xff0c;\u7a0b\u5e8f\u4f1a\u63d0\u793a\u6b63\u786e\u7684\u4f7f\u7528\u65b9\u6cd5\u5e76\u9000\u51fa\u3002<\/li>\n<hr id=\"aBzx2\" \/>\n<p id=\"u2ae6c27d\">\u6211\u4eec\u53d1\u73b0udp\u3001tcp\u3001http\u6240\u6709\u7684\u5e95\u5c42\u903b\u8f91\u90fd\u662f\u5dee\u4e0d\u591a\u7684&#xff0c;\u800c\u6211\u4eec\u53ea\u8981\u5199\u4e0a\u5c42\u903b\u8f91\u5c31\u597d\u4e86\u3002<\/p>\n<p id=\"u3c7aa925\">\u8fd9\u91cc\u6211\u4eec\u4e3b\u8981\u8bf4\u539f\u7406&#xff0c;\u4e0b\u97621-5\u7684\u5de5\u4f5c\u6211\u4eec\u90fd\u4e0d\u505a\u4e86&#xff0c;\u6240\u4ee5httpRequest&#xff0c;httpResponse\u4e5f\u90fd\u7ed9\u4e00\u4e2a\u7f13\u51b2\u533a\u5c31\u884c\u4e86\u3002<\/p>\n<p id=\"u2c3d40b6\"><span style=\"color:#fe2c24\">callback \u7684\u662f Get \u51fd\u6570<\/span><\/p>\n<p id=\"ua31fe293\">\u4e0b\u9762\u6211\u4eec\u7528\u6d4f\u89c8\u5668\u5145\u5f53\u5ba2\u6237\u7aef\u53d1\u8d77\u8bf7\u6c42\u770b\u4e00\u4e0b\u7ed3\u679c<\/p>\n<p id=\"uc8ff7946\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"683\" id=\"uacfc00d4\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091906-6802190a4c1a6.png\" width=\"1200\" \/><\/p>\n<p id=\"u545b541b\">\u65e0\u6cd5\u8bbf\u95ee&#xff0c;\u6211\u4eec\u6765\u5f00\u653e\u4e00\u4e0b\u7aef\u53e3\u53f7&#xff0c;\u817e\u8baf\u4e91\u53ef\u4ee5\u76f4\u63a5\u5728\u5c0f\u7a0b\u5e8f\u4e0a\u5f00&#xff0c;\u5c31\u8fd8\u633a\u65b9\u4fbf\u7684~<\/p>\n<p id=\"u961d91f8\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1182\" id=\"u741b1244\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091906-6802190a94547.jpg\" width=\"1200\" \/><\/p>\n<p id=\"ub4f29f59\">\u7136\u540e\u5c31\u53ef\u4ee5\u770b\u5230<\/p>\n<p id=\"u5e9df99b\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"418\" id=\"ua473b6ce\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091907-6802190b215ed.png\" width=\"981\" \/><\/p>\n<p id=\"ubf7fc48f\">\u62a5\u5934\u6211\u4eec\u6682\u65f6\u4e0d\u8981\u540e\u9762\u6162\u6162\u586b&#xff0c;\u6b63\u6587\u90e8\u5206\u6211\u4eec\u641e\u4e00\u4e2a\u7f51\u9875\u3002 \u7f51\u9875\u4e0d\u4f1a\u5199&#xff0c;\u53ef\u4ee5\u641c\u4e00\u4e0b<span style=\"color:#fe2c24\">w3cschool html\u6559\u7a0b<\/span><\/p>\n<p id=\"u02b991ae\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1200\" id=\"u8b121b4e\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091907-6802190b4775d.png\" width=\"1200\" \/><\/p>\n<p id=\"u0f9779dc\">\u8fd9\u91cc\u6211\u4eec\u5148\u5199\u5230Get\u51fd\u6570\u91cc&#xff0c;\u540e\u9762\u6211\u4eec\u5728\u5206\u79bb<\/p>\n<p>void Get(const httpRequest &amp;req, httpResponse &amp;resp)<br \/>\n{<\/p>\n<p>    cout &lt;&lt; &#034;&#8212;&#8212;&#8212;&#8212;&#8212;-http start&#8212;&#8212;&#8212;&#8212;&#8212;&#034; &lt;&lt; endl;<br \/>\n    cout &lt;&lt; req.inbuffer &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;&#8212;&#8212;&#8212;&#8212;&#8212;-http end&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#034; &lt;&lt; endl;<\/p>\n<p>     string respline &#061; &#034;HTTP\/1.1 200 OK\\\\r\\\\n&#034;;<br \/>\n    \/\/ string respheader;<br \/>\n     string respblank &#061; &#034;\\\\r\\\\n&#034;;<\/p>\n<p>\/\/\u968f\u4fbf\u505a\u4e00\u4e2a\u7f51\u9875<br \/>\n     string body&#061;&#034;&lt;html lang&#061;\\\\&#034;en\\\\&#034;&gt;&lt;head&gt;&lt;meta charset&#061;\\\\&#034;UTF-8\\\\&#034;&gt;&lt;title&gt;for test&lt;\/title&gt;&lt;h1&gt;hello world&lt;\/h1&gt;&lt;\/head&gt;&lt;body&gt;&lt;p&gt;\u4f60\u597d\u5440 \u795d\u4f60\u5929\u5929\u5f00\u5fc3~&lt;\/p&gt;&lt;\/body&gt;&lt;\/html&gt;&#034;;<\/p>\n<p>\/\/\u5e8f\u5217\u5316<br \/>\n     resp.outbuffer &#043;&#061; respline;<br \/>\n     resp.outbuffer &#043;&#061; respblank;<br \/>\n     resp.outbuffer &#043;&#061; body;<br \/>\n} <\/p>\n<p id=\"ub2fef3fc\">\u867d\u7136\u6211\u4eec\u5728\u54cd\u5e94\u7684\u65f6\u5019\u6ca1\u6709\u5e26\u54cd\u5e94\u62a5\u5934&#xff0c;\u4f46\u662f\u6211\u4eec\u7684\u6d4f\u89c8\u5668\u4f9d\u65e7\u662f\u80fd\u8bc6\u522b\u7684&#xff0c;\u8fd9\u91cc\u60f3\u8bf4\u7684\u662f\u73b0\u5728\u6d4f\u89c8\u5668\u5f88\u667a\u80fd\u4e86&#xff0c;\u53ef\u4ee5\u4e0d\u7528\u544a\u8bc9\u5b83\u6b63\u6587\u662f\u4ec0\u4e48\u4e5f\u53ef\u4ee5\u6839\u636e\u6b63\u6587\u5185\u5bb9\u8bc6\u522b\u8fd9\u662f\u4ec0\u4e48\u4e1c\u897f&#xff0c;\u4f46\u662f\u6709\u7684\u6d4f\u89c8\u5668\u505a\u4e0d\u5230\u3002\u8fd9\u91cc\u6211\u4eec\u7528\u7684\u662fchrome\u6d4f\u89c8\u5668\u3002<\/p>\n<p id=\"u7e1c166e\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"543\" id=\"uc87d50ee\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091908-6802190c9c5a7.png\" width=\"915\" \/><\/p>\n<p id=\"ubf640c78\">\u5982\u679c\u6211\u4eec\u8981\u52a0\u4e00\u4e2a\u62a5\u5934\u91cc\u9762\u53ef\u4ee5\u5e26\u4e00\u4e9b\u5c5e\u6027\u5462&#xff1f;<\/p>\n<p id=\"u0d697a08\">\u5982Content-Type &#xff0c;\u544a\u8bc9\u522b\u4eba\u8fd4\u56de\u7684\u662f\u4ec0\u4e48\u8d44\u6e90\u3002\u7f51\u4e0a\u53ef\u4ee5\u641c\u4e00\u4e0b<span style=\"color:#fe2c24\">Content-Type \u5bf9\u7167\u8868&#xff0c;<\/span>\u6765\u8fdb\u884c\u6dfb\u52a0<\/p>\n<p id=\"u0e647dd5\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1200\" id=\"u9805a0ff\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091908-6802190cc1ec0.png\" width=\"1200\" \/><\/p>\n<h2 id=\"lGxJd\">\u4e09.\u670d\u52a1\u5668\u548c\u7f51\u9875\u5206\u79bb<\/h2>\n<p id=\"uad6fa6e6\">\u7b80\u5355\u5b9e\u73b0\u4e4b\u540e&#xff0c;\u6211\u4eec\u6765\u89e3\u51b3<span style=\"color:#4da8ee\">\u670d\u52a1\u5668\u548c\u7f51\u9875\u5206\u79bb<\/span>&#xff0c;\u7136\u540e\u901a\u8fc7\u670d\u52a1\u5668\u628a\u7f51\u9875\u8fd4\u56de<\/p>\n<p id=\"u9e974a1b\">\u5f15\u5165&#xff1a;<\/p>\n<p id=\"u4df627de\">\u5728C&#043;&#043;\u4e2d&#xff0c;istringstream \u7c7b\u662f\u5728 &lt;sstream&gt; \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684&#xff0c;\u6240\u4ee5\u4f60\u9700\u8981\u5305\u542b\u8fd9\u4e2a\u5934\u6587\u4ef6\u6765\u4f7f\u7528 istringstream \u5bf9\u8c61\u3002\u4e0b\u9762\u662f\u5982\u4f55\u5728\u4ee3\u7801\u4e2d\u5305\u542b\u5b83\u7684\u793a\u4f8b&#xff1a;<\/p>\n<p>#include &lt;sstream&gt;<br \/>\nint main() {<br \/>\n    std::string line &#061; &#034;\u4e00\u4e9b\u6587\u672c&#034;;<br \/>\n    std::istringstream iss(line);<br \/>\n    \/\/ &#8230; \u4f7f\u7528 iss &#8230;<br \/>\n    return 0;<br \/>\n} <\/p>\n<p id=\"u8f3a8513\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#xff0c;istringstream \u88ab\u7528\u6765\u4ece\u5b57\u7b26\u4e32 line \u4e2d\u8bfb\u53d6\u6570\u636e&#xff0c;\u5c31\u50cf\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u4e00\u6837\u3002<\/p>\n<p id=\"u6737e002\">\u8fd0\u7528\u66f4\u65b0&#xff1a;<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;sstream&gt;<br \/>\nusing namespace std;<\/p>\n<p>class Util<br \/>\n{<br \/>\npublic:<br \/>\n    \/\/ xxx yyy zzz\\\\r\\\\naaa<br \/>\n    static string GetOneline(string &amp;buffer, const string &amp;sep)<br \/>\n    {<br \/>\n        auto pos &#061; buffer.find(sep);<br \/>\n        if (pos &#061;&#061; string::npos)<br \/>\n            return &#034;&#034;;<br \/>\n        string sub &#061; buffer.substr(0, pos);<br \/>\n        return sub;<br \/>\n    }<br \/>\n};<\/p>\n<p>const string sep &#061; &#034;\\\\r\\\\n&#034;;\/\/\u5207\u5272\u7b26<\/p>\n<p>class httpRequest<br \/>\n{<\/p>\n<p>public:<br \/>\n    httpRequest(){};<br \/>\n    ~httpRequest(){};<\/p>\n<p>    void parse()<br \/>\n    {<br \/>\n        \/\/ 1. \u4eceinbuffer\u4e2d\u62ff\u5230\u7b2c\u4e00\u884c&#xff0c;\u5206\u9694\u7b26\\\\r\\\\n<br \/>\n        string line &#061; Util::GetOneline(inbuffer, sep);<br \/>\n        if (line.empty())<br \/>\n            return;<\/p>\n<p>        \/\/ 2. \u4ece\u8bf7\u6c42\u884c\u4e2d\u63d0\u53d6\u4e09\u4e2a\u5b57\u6bb5<br \/>\n        istringstream iss(line);<br \/>\n        iss &gt;&gt; method &gt;&gt; url &gt;&gt; httpversion;<\/p>\n<p>    }<\/p>\n<p>public:<br \/>\n    string inbuffer;<br \/>\n    \/\/ string reqline;<br \/>\n    \/\/ vector&lt;std::string&gt; reqheader;<br \/>\n    \/\/ string body;<\/p>\n<p>    string method;<br \/>\n    string url;<br \/>\n    string httpversion;<br \/>\n};<\/p>\n<p>\/\/\u670d\u52a1\u5668<br \/>\nclass httpResponse<br \/>\n{<\/p>\n<p>public:<br \/>\n    string outbuffer;\/\/\u7f13\u51b2\u533a<br \/>\n};<br \/>\n#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>class Util<br \/>\n{<br \/>\npublic:<br \/>\n    \/\/ xxx yyy zzz\\\\r\\\\naaa<br \/>\n    static string GetOneline(string &amp;buffer, const string &amp;sep)<br \/>\n    {<br \/>\n        auto pos &#061; buffer.find(sep);<br \/>\n        if (pos &#061;&#061; string::npos)<br \/>\n            return &#034;&#034;;<br \/>\n        string sub &#061; buffer.substr(0, pos);<br \/>\n        return sub;<br \/>\n    }<br \/>\n}; <\/p>\n<p id=\"ud9e52748\">\u4ec0\u4e48\u662fweb\u6839\u76ee\u5f55&#xff1f;<\/p>\n<p id=\"uab88cb40\">\u5b9e\u9645\u4e0a\u672a\u6765\u4e00\u4e2aweb\u670d\u52a1\u5668\u5199\u597d\u4e4b\u540e&#xff0c;\u53ef\u4e0d\u4ec5\u4ec5\u6709\u8fd9\u4e9b\u4ee3\u7801\u3002\u6bcf\u4e00\u4e2aweb\u670d\u52a1\u5668\u90fd\u6709web\u6839\u76ee\u5f55&#xff0c;\u672a\u6765\u6240\u6709\u56fe\u7247\u3001\u89c6\u9891\u3001\u97f3\u9891\u7b49\u5404\u79cdweb\u8d44\u6e90\u90fd\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b&#xff0c;\u6309\u7167\u76ee\u5f55\u7ed3\u6784\u7ec4\u7ec7\u53f7\u597d&#xff0c;\u672a\u6765\u60f3\u8bf7\u6c42\u8d44\u6e90\u5c31\u4eceurl\u8bf7\u6c42\u3002\u90a3\u5982\u4f55\u4fdd\u8bc1\u6309\u7167\u6211\u4eec\u7684\u9700\u6c42\u5728\u6307\u5b9a\u8def\u5f84\u4e0b\u53bb\u5bfb\u627e\u5462&#xff1f;<\/p>\n<p id=\"u59fa76fa\">\u8bbe\u8ba1\u5982\u4e0b\u76ee\u5f55<\/p>\n<p id=\"ufddf5bf9\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"614\" id=\"uc17832c4\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091910-6802190e1bf1e.png\" width=\"710\" \/><\/p>\n<p id=\"ud2e43c06\">err.html<\/p>\n<p>&lt;!doctype html&gt;<br \/>\n&lt;html lang&#061;&#034;en&#034;&gt;<\/p>\n<p>  &lt;head&gt;<br \/>\n    &lt;meta charset&#061;&#034;UTF-8&#034;&gt;<br \/>\n    &lt;title&gt;404 Not Found&lt;\/title&gt;<br \/>\n    &lt;style&gt;<br \/>\n      body {<br \/>\n        text-align: center;<br \/>\n        padding: 150px;<br \/>\n      }<\/p>\n<p>      h1 {<br \/>\n        font-size: 50px;<br \/>\n      }<\/p>\n<p>      body {<br \/>\n        font-size: 20px;<br \/>\n      }<\/p>\n<p>      a {<br \/>\n        color: #008080;<br \/>\n        text-decoration: none;<br \/>\n      }<\/p>\n<p>      a:hover {<br \/>\n        color: #005F5F;<br \/>\n        text-decoration: underline;<br \/>\n      }<br \/>\n    &lt;\/style&gt;<br \/>\n  &lt;\/head&gt;<\/p>\n<p>  &lt;body&gt;<br \/>\n    &lt;div&gt;<br \/>\n      &lt;h1&gt;404&lt;\/h1&gt;<br \/>\n      &lt;p&gt;\u9875\u9762\u672a\u627e\u5230&lt;br&gt;&lt;\/p&gt;<br \/>\n      &lt;p&gt;<br \/>\n        \u60a8\u8bf7\u6c42\u7684\u9875\u9762\u53ef\u80fd\u5df2\u7ecf\u88ab\u5220\u9664\u3001\u66f4\u540d\u6216\u8005\u60a8\u8f93\u5165\u7684\u7f51\u5740\u6709\u8bef\u3002&lt;br&gt;<br \/>\n        \u8bf7\u5c1d\u8bd5\u4f7f\u7528\u4ee5\u4e0b\u94fe\u63a5\u6216\u8005\u81ea\u884c\u641c\u7d22:&lt;br&gt;&lt;br&gt;<br \/>\n        &lt;a href&#061;&#034;https:\/\/www.baidu.com&#034;&gt;\u767e\u5ea6\u4e00\u4e0b&gt;&lt;\/a&gt;<br \/>\n      &lt;\/p&gt;<br \/>\n    &lt;\/div&gt;<br \/>\n  &lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt; <\/p>\n<p id=\"ucb29fc87\">index.html:<\/p>\n<p>&lt;!DOCTYPE html&gt;<br \/>\n&lt;html lang&#061;&#034;en&#034;&gt;<\/p>\n<p>  &lt;head&gt;<br \/>\n    &lt;meta charset&#061;&#034;UTF-8&#034;&gt;<br \/>\n    &lt;meta name&#061;&#034;viewport&#034; content&#061;&#034;width&#061;device-width, initial-scale&#061;1.0&#034;&gt;<br \/>\n    &lt;title&gt;Document&lt;\/title&gt;<br \/>\n  &lt;\/head&gt;<\/p>\n<p>  &lt;body&gt;<br \/>\n    &lt;!&#8211; &lt;form action&#061;&#034;\/a\/b\/hello.html&#034; method&#061;&#034;post&#034;&gt;<br \/>\n    name: &lt;input type&#061;&#034;text&#034; name&#061;&#034;name&#034;&gt;&lt;br&gt;<br \/>\n    password: &lt;input type&#061;&#034;password&#034; name&#061;&#034;passwd&#034;&gt;&lt;br&gt;<br \/>\n    &lt;input type&#061;&#034;submit&#034; value&#061;&#034;\u63d0\u4ea4&#034;&gt;<br \/>\n  &lt;\/form&gt; &#8211;&gt;<br \/>\n    &lt;h1&gt;\u8fd9\u4e2a\u662f\u6211\u4eec\u7684\u9996\u9875&lt;\/h1&gt;<br \/>\n    &lt;!&#8211; &lt;img src&#061;&#034;\/image\/1.png&#034; alt&#061;&#034;\u8fd9\u662f\u4e00\u76f4\u732b&#034; width&#061;&#034;100&#034; height&#061;&#034;100&#034;&gt; \u6839\u636esrc\u5411\u6211\u4eec\u7684\u670d\u52a1\u5668\u6d4f\u89c8\u5668\u81ea\u52a8\u53d1\u8d77\u4e8c\u6b21\u8bf7\u6c42 &#8211;&gt;<br \/>\n    &lt;!&#8211; &lt;img src&#061;&#034;\/image\/2.jpg&#034; alt&#061;&#034;\u8fd9\u662f\u82b1&#034;&gt; &#8211;&gt;<br \/>\n  &lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p id=\"u516bbff7\">\u5b9e\u73b0\u5206\u79bb&#xff1a;<\/p>\n<p id=\"ubed8f192\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"422\" id=\"u0f09727f\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091910-6802190e3178e.png\" width=\"903\" \/><\/p>\n<p id=\"u38434942\">\u73b0\u5728\u6211\u4eec\u7ed9\u7f51\u9875\u6dfb\u52a0\u4e00\u4e0b\u529f\u80fd&#xff0c;\u6bd4\u5982\u8bf4\u7f51\u9875\u662f\u652f\u6301\u70b9\u51fb\u7136\u540e\u8df3\u8f6c\u94fe\u63a5\u7684<\/p>\n<p id=\"ue5a653dc\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"454\" id=\"u888b17c6\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091910-6802190e50e24.png\" width=\"927\" \/><\/p>\n<p id=\"u3b733af6\">\u8df3\u8f6c\u6210\u529f\u5566~<\/p>\n<p id=\"u8a2c296f\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"330\" id=\"uf49fefc6\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091910-6802190e70eae.png\" width=\"648\" \/><\/p>\n<hr id=\"nkU7Q\" \/>\n<h3 id=\"wWHZh\" style=\"background-color:transparent\">\u601d\u8003\u4e0e\u8865\u5145&#xff1a;<\/h3>\n<p id=\"u9043536b\">1.\u8bf7\u6c42\u548c\u54cd\u5e94\u600e\u4e48\u4fdd\u8bc1\u5e94\u7528\u5c42\u5b8c\u6574\u8bfb\u53d6\u5b8c\u6bd5\u4e86\u5462&#xff1f;<\/p>\n<p id=\"u7858fa55\">\u9996\u5148\u6211\u4eec\u53d1\u73b0http\u8bf7\u6c42\u90fd\u662f\u5b57\u7b26\u4e32\u6309\u884c\u4e3a\u5355\u4f4d&#xff0c;\u6240\u4ee5<\/p>\n<li id=\"u3ef3e108\">\u6211\u53ef\u4ee5\u8bfb\u53d6\u5b8c\u6574\u7684\u4e00\u884c<\/li>\n<li id=\"uaa425a94\">while(\u8bfb\u53d6\u5b8c\u6574\u4e00\u884c) &#8211;&gt; \u6240\u6709\u7684\u8bf7\u6c42\u884c&#043;\u8bf7\u6c42\u884c\u62a5\u6587\u5168\u90e8\u8bfb\u5b8c &#8211;&gt; \u76f4\u5230\u7a7a\u884c&#xff01;<\/li>\n<li id=\"u6bd7465d\">\u6211\u4eec\u6ca1\u8bf4\u6b63\u6587\u4e5f\u662f\u6309\u884c\u4e3a\u5355\u4f4d\u5206\u5f00\u7684\u6ca1\u6709\u529e\u6cd5\u4fdd\u8bc1\u628a\u6b63\u6587\u8bfb\u5b8c&#xff0c;\u4f46\u662f\u6211\u4eec\u80fd\u4fdd\u8bc1\u628a\u62a5\u5934\u8bfb\u5b8c&#xff0c;\u800c\u62a5\u5934\u91cc\u6709\u4e00\u4e2aContent-Length&#xff1a;xxx&#xff08;\u4ee3\u8868\u6b63\u6587\u957f\u5ea6&#xff09;<\/li>\n<li id=\"u74e86797\">\u89e3\u6790\u51fa\u6765\u5185\u5bb9\u957f\u5ea6&#xff0c;\u5728\u6839\u636e\u5185\u5bb9\u957f\u5ea6&#xff0c;\u8bfb\u53d6\u6b63\u6587\u5373\u53ef&#xff01;<\/li>\n<p id=\"u9e0caa55\">\n<p id=\"ub879f598\">2.\u8bf7\u6c42\u548c\u54cd\u5e94\u662f\u600e\u4e48\u505a\u5230\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684&#xff1f;<\/p>\n<ul>\n<li id=\"u6567ea34\"><span style=\"color:#4da8ee\">http\u662f\u7528\u7684\u7279\u6b8a\u5b57\u7b26\u81ea\u5df1\u5b9e\u73b0\u7684\u3002http\u5e8f\u5217\u5316\u4ec0\u4e48\u90fd\u4e0d\u505a\u76f4\u63a5\u53d1\u5c31\u597d\u4e86&#xff0c;\u53cd\u5e8f\u5217\u5316 &#xff1a;\u7b2c\u4e00\u884c&#043;\u8bf7\u6c42\/\u54cd\u5e94\u62a5\u5934&#xff0c;\u53ea\u8981\u6309\u7167\\\\r\\\\n\u5c06\u5b57\u7b26\u4e321-&gt;n\u5373\u53ef&#xff01;\u4e0d\u7528\u501f\u52a9\u4efb\u4f55\u4e1c\u897f\u5982Json<\/span><\/li>\n<li id=\"ue47c57af\">protobuf\u7b49\u3002\u800c\u6b63\u6587\u5e8f\u5217\u5316\u53cd\u5e8f\u5217\u4e5f\u4e0d\u7528\u505a\u76f4\u63a5\u53d1\u9001\u5c31\u884c\u4e86\u3002\u5982\u679c\u4f60\u7684\u6b63\u6587\u643a\u5e26\u7ed3\u6784\u5316\u6570\u636e\u5c31\u8981\u81ea\u5df1\u5904\u7406\u4e86\u3002<\/li>\n<li id=\"uc81fdf0d\">\u4e0a\u9762\u6211\u4eec\u4e5f\u901a\u8fc7\u5199\u4ee3\u7801\u7684\u65b9\u5f0f&#xff0c;\u9a8c\u8bc1\u4e0a\u9762\u8bf4\u7684\u4e1c\u897f\u3002<\/li>\n<li id=\"u3ccabe26\">\u4ee5\u524d\u5199udp\u548ctcp\u6211\u4eec\u90fd\u5199\u8fc7\u670d\u52a1\u7aef\u7528\u8fc7\u5957\u63a5\u5b57&#xff0c;\u8fd9\u91cc\u8fd8\u662f\u76f4\u63a5\u62ff\u8fc7\u6765\u7528\u5566<\/li>\n<\/ul>\n<p id=\"u080af19b\">\n<p id=\"ud5f6823c\">3. \u5982\u4f55\u76d1\u89c6\u67e5\u770b\u7f51\u9875\u7aef<\/p>\n<p id=\"u3f4c80f8\">\u6309\u56fe\u7247\u64cd\u7eb5\u5373\u53ef<\/p>\n<p id=\"u014a1227\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1198\" id=\"uc73baa5f\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091910-6802190e80767.png\" width=\"1200\" \/><\/p>\n<p id=\"uffee63e4\">\u5c31\u53ef\u4ee5\u67e5\u770b\u5230\u5566<\/p>\n<p id=\"u3dd70ea4\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"789\" id=\"u4bc40d36\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091911-6802190f46b92.png\" width=\"1200\" \/><\/p>\n<p id=\"uc05db571\">\u4e0b\u7bc7\u6587\u7ae0\u5c06\u7ee7\u7eed\u8bb2\u89e3\u7f51\u9875\u5bf9\u56fe\u7247\u7684\u63d2\u5165&#xff0c;\u548c http \u8bbe\u8ba1\u7684\u8be6\u7ec6\u89e3\u8bfb~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb6.1k\u6b21\uff0c\u70b9\u8d5e77\u6b21\uff0c\u6536\u85cf71\u6b21\u3002\u672c\u6587\u5c06\u4ece\u6784\u5efa\u6700\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668\u5f00\u59cb\uff0c\u9010\u6b65\u6df1\u5165\u5230\u670d\u52a1\u56682.0\u7248\u672c\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u5305\u62ec\u534f\u8bae\u5904\u7406\u3001\u5b50\u8fdb\u7a0b\u7ba1\u7406\u7b49\u5173\u952e\u6280\u672f\u70b9\uff0c\u5e76\u63a2\u8ba8\u670d\u52a1\u5668\u4e0e\u7f51\u9875\u5206\u79bb\u7684\u8bbe\u8ba1\u601d\u8def\u53ca\u5176\u91cd\u8981\u6027\u3002<\/p>\n","protected":false},"author":2,"featured_media":9277,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[304,55,303,292,43,305,191],"topic":[],"class_list":["post-9290","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-c","tag-linux-os","tag-292","tag-43","tag-305","tag-191"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Linux#58][HTTP] \u81ea\u5df1\u6784\u5efa\u670d\u52a1\u5668 | \u5b9e\u73b0\u7f51\u9875\u5206\u79bb | \u8bbe\u8ba1\u601d\u8def - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/9290.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Linux#58][HTTP] \u81ea\u5df1\u6784\u5efa\u670d\u52a1\u5668 | \u5b9e\u73b0\u7f51\u9875\u5206\u79bb | \u8bbe\u8ba1\u601d\u8def - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb6.1k\u6b21\uff0c\u70b9\u8d5e77\u6b21\uff0c\u6536\u85cf71\u6b21\u3002\u672c\u6587\u5c06\u4ece\u6784\u5efa\u6700\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668\u5f00\u59cb\uff0c\u9010\u6b65\u6df1\u5165\u5230\u670d\u52a1\u56682.0\u7248\u672c\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u5305\u62ec\u534f\u8bae\u5904\u7406\u3001\u5b50\u8fdb\u7a0b\u7ba1\u7406\u7b49\u5173\u952e\u6280\u672f\u70b9\uff0c\u5e76\u63a2\u8ba8\u670d\u52a1\u5668\u4e0e\u7f51\u9875\u5206\u79bb\u7684\u8bbe\u8ba1\u601d\u8def\u53ca\u5176\u91cd\u8981\u6027\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/9290.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T09:19:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091904-68021908f3a80.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/9290.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/9290.html\",\"name\":\"[Linux#58][HTTP] \u81ea\u5df1\u6784\u5efa\u670d\u52a1\u5668 | \u5b9e\u73b0\u7f51\u9875\u5206\u79bb | \u8bbe\u8ba1\u601d\u8def - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T09:19:12+00:00\",\"dateModified\":\"2025-04-18T09:19:12+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/9290.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/9290.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/9290.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Linux#58][HTTP] \u81ea\u5df1\u6784\u5efa\u670d\u52a1\u5668 | \u5b9e\u73b0\u7f51\u9875\u5206\u79bb | \u8bbe\u8ba1\u601d\u8def\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Linux#58][HTTP] \u81ea\u5df1\u6784\u5efa\u670d\u52a1\u5668 | \u5b9e\u73b0\u7f51\u9875\u5206\u79bb | \u8bbe\u8ba1\u601d\u8def - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/9290.html","og_locale":"zh_CN","og_type":"article","og_title":"[Linux#58][HTTP] \u81ea\u5df1\u6784\u5efa\u670d\u52a1\u5668 | \u5b9e\u73b0\u7f51\u9875\u5206\u79bb | \u8bbe\u8ba1\u601d\u8def - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb6.1k\u6b21\uff0c\u70b9\u8d5e77\u6b21\uff0c\u6536\u85cf71\u6b21\u3002\u672c\u6587\u5c06\u4ece\u6784\u5efa\u6700\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668\u5f00\u59cb\uff0c\u9010\u6b65\u6df1\u5165\u5230\u670d\u52a1\u56682.0\u7248\u672c\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u5305\u62ec\u534f\u8bae\u5904\u7406\u3001\u5b50\u8fdb\u7a0b\u7ba1\u7406\u7b49\u5173\u952e\u6280\u672f\u70b9\uff0c\u5e76\u63a2\u8ba8\u670d\u52a1\u5668\u4e0e\u7f51\u9875\u5206\u79bb\u7684\u8bbe\u8ba1\u601d\u8def\u53ca\u5176\u91cd\u8981\u6027\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/9290.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T09:19:12+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418091904-68021908f3a80.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/9290.html","url":"https:\/\/www.wsisp.com\/helps\/9290.html","name":"[Linux#58][HTTP] \u81ea\u5df1\u6784\u5efa\u670d\u52a1\u5668 | \u5b9e\u73b0\u7f51\u9875\u5206\u79bb | \u8bbe\u8ba1\u601d\u8def - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T09:19:12+00:00","dateModified":"2025-04-18T09:19:12+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/9290.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/9290.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/9290.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"[Linux#58][HTTP] \u81ea\u5df1\u6784\u5efa\u670d\u52a1\u5668 | \u5b9e\u73b0\u7f51\u9875\u5206\u79bb | \u8bbe\u8ba1\u601d\u8def"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/9290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=9290"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/9290\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/9277"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=9290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=9290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=9290"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=9290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}