{"id":4219,"date":"2020-04-03T15:02:42","date_gmt":"2020-04-03T07:02:42","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4219"},"modified":"2020-04-03T15:02:42","modified_gmt":"2020-04-03T07:02:42","slug":"curl-for-people-%ef%bc%88crp%ef%bc%89%e5%ba%93%e4%bb%8b%e7%bb%8d-%e9%ab%98%e7%ba%a7%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4219","title":{"rendered":"Curl for People \uff08crp\uff09\u5e93\u4ecb\u7ecd\u2014\u2014\u9ad8\u7ea7\u7528\u6cd5"},"content":{"rendered":"<p><a href=\"https:\/\/blog.csdn.net\/wanggao_1990\/article\/details\/79806384?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4&amp;utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>\u539f\u521b\u738b\u9ad8_1990 \u6700\u540e\u53d1\u5e03\u4e8e2018-04-03 17:21:50 \u9605\u8bfb\u6570 644 \u6536\u85cf<br \/>\n\u5c55\u5f00<br \/>\n1\u3001\u54cd\u5e94\u5bf9\u8c61 \uff08Response Objects\uff09<br \/>\nResponse\u5bf9\u8c61\u662f\u6570\u636e\u5305\u3002\u4ed6\u4eec\u7684\u552f\u4e00\u76ee\u7684\u662f\u5728\u8bf7\u6c42\u7ed3\u675f\u65f6\u63d0\u4f9b\u5ba2\u6237\u7aef\u4fe1\u606f &#8211; \u5728API Response\u8fd4\u56de\u7ed9\u60a8\u4e4b\u540e\uff0c\u6ca1\u6709\u4efb\u4f55API\u4f7f\u7528\u5b83\u3002\u8fd9\u4e2a\u63a8\u7406\u4fc3\u4f7f\u51b3\u5b9a\u4f7f\u54cd\u5e94\u7684\u6210\u5458\u5b57\u6bb5\u516c\u5f00\u548c\u53ef\u53d8\u3002<br \/>\n\u4e00\u4e2aResponse\u6709\u4e00\u4e0b\u51e0\u4e2a\u9886\u57df\uff1a<\/p>\n<p>long status_code; \/\/ The HTTP status code for the request<br \/>\nstd::string text; \/\/ The body of the HTTP response<br \/>\nHeader header; \/\/ A map-like collection of the header fields<br \/>\nUrl url; \/\/ The effective URL of the ultimate request<br \/>\ndouble elapsed; \/\/ The total time of the request in seconds<br \/>\nCookies cookies; \/\/ A map-like collection of cookies returned in the request<\/p>\n<p>\u800c\u4e14\u8bbf\u95ee\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<p>auto r = cpr::Get(cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<br \/>\nif (r.status_code &gt;= 400) {<br \/>\nstd::cerr &lt;&lt; &#8220;Error [&#8221; &lt;&lt; r.status_code &lt;&lt; &#8220;] making request&#8221; &lt;&lt; std::endl;<br \/>\n} else {<br \/>\nstd::cout &lt;&lt; &#8220;Request took &#8221; &lt;&lt; r.elapsed &lt;&lt; std::endl;<br \/>\nstd::cout &lt;&lt; &#8220;Body:&#8221; &lt;&lt; std::endl &lt;&lt; r.text;<br \/>\n}<\/p>\n<p>\u8fd9Header\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u5177\u6709\u91cd\u8981\u4fee\u6539\u7684\u5730\u56fe\u3002\u6309\u7167RFC 7230\u7684\u8981\u6c42\uff0c\u5176\u5bc6\u94a5\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff1a<\/p>\n<p>auto r = cpr::Get(cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<br \/>\nstd::cout &lt;&lt; r.header[&#8220;content-type&#8221;] &lt;&lt; std::endl;<br \/>\nstd::cout &lt;&lt; r.header[&#8220;Content-Type&#8221;] &lt;&lt; std::endl;<br \/>\nstd::cout &lt;&lt; r.header[&#8220;CoNtEnT-tYpE&#8221;] &lt;&lt; std::endl;<\/p>\n<p>\u6240\u6709\u8fd9\u4e9b\u5e94\u8be5\u6253\u5370\u76f8\u540c\u7684\u503c\u201dapplication\/json\u201d\u3002\u7c7b\u4f3c\u5730\uff0cCookie\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7c7b\u4f3c\u5730\u56fe\u7684\u754c\u9762\u8bbf\u95ee\uff0c\u4f46\u5b83\u4eec\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff1a<\/p>\n<p>auto r = cpr::Get(cpr::Url{&#8220;http:\/\/www.httpbin.org\/cookies\/set?cookies=yummy&#8221;});<br \/>\nstd::cout &lt;&lt; r.cookies[&#8220;cookies&#8221;] &lt;&lt; std::endl; \/\/ Prints yummy<br \/>\nstd::cout &lt;&lt; r.cookies[&#8220;Cookies&#8221;] &lt;&lt; std::endl; \/\/ Prints nothing<\/p>\n<p>\u6b63\u5982\u4f60\u6240\u770b\u5230\u7684\uff0c\u8fd9\u4e2aResponse\u5bf9\u8c61\u662f\u5b8c\u5168\u900f\u660e\u7684\u3002\u5b83\u7684\u6240\u6709\u6570\u636e\u5b57\u6bb5\u90fd\u53ef\u4ee5\u968f\u65f6\u8bbf\u95ee\uff0c\u56e0\u4e3a\u5b83\u53ea\u6709\u5728\u6709\u4fe1\u606f\u53ef\u4ee5\u6c9f\u901a\u7684\u65f6\u5019\u624d\u6709\u7528\uff0c\u6240\u4ee5\u5f53\u4f60\u5b8c\u6210\u5b83\u65f6\uff0c\u53ef\u4ee5\u8ba9\u5b83\u5b89\u5168\u5730\u8d85\u51fa\u8303\u56f4\u3002<\/p>\n<p>2\u3001\u8bf7\u6c42\u6807\u9898 \uff08Request Headers\uff09<br \/>\n\u8bf4\u5230\u8fd9\u4e2aHeader\uff0c\u4f60\u53ef\u4ee5\u5728\u8bf7\u6c42\u8c03\u7528\u4e2d\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u6807\u9898\u3002\u8be5\u5bf9\u8c61\u5b8c\u5168\u76f8\u540c\uff1a<\/p>\n<p>auto r = cpr::Get(cpr::Url{&#8220;http:\/\/www.httpbin.org\/headers&#8221;},<br \/>\ncpr::Header{{&#8220;accept&#8221;, &#8220;application\/json&#8221;}});<br \/>\nstd::cout &lt;&lt; r.text &lt;&lt; std::endl;<\/p>\n<p>\/*<br \/>\n* &#8220;headers&#8221;: {<br \/>\n* &#8220;Accept&#8221;: &#8220;application\/json&#8221;,<br \/>\n* &#8220;Host&#8221;: &#8220;www.httpbin.org&#8221;,<br \/>\n* &#8220;User-Agent&#8221;: &#8220;curl\/7.42.0-DEV&#8221;<br \/>\n* }<br \/>\n*\/<\/p>\n<p>\u60a8\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027Header\uff0cParameters\uff0cPayload\uff0c\u548cMultipart\u3002\u4ed6\u4eec\u90fd\u6709\u4ee5\u4e0b\u5f62\u5f0f\u7684\u6784\u9020\u51fd\u6570\uff1a<\/p>\n<p>auto header = cpr::Header{{&#8220;header-key&#8221;, &#8220;header-value&#8221;}};<br \/>\nauto parameters = cpr::Parameters{{&#8220;parameter-key&#8221;, &#8220;parameter-value&#8221;}};<br \/>\nauto payload = cpr::Payload{{&#8220;payload-key&#8221;, &#8220;payload-value&#8221;}};<br \/>\nauto multipart = cpr::Multipart{{&#8220;multipart-key&#8221;, &#8220;multipart-value&#8221;}};<\/p>\n<p>\u8fd9\u4e0d\u662f\u5076\u7136\u7684 &#8211; \u6240\u6709\u8fd9\u4e9b\u90fd\u662f\u7c7b\u5730\u56fe\u5bf9\u8c61\uff0c\u5b83\u4eec\u7684\u8bed\u6cd5\u662f\u76f8\u540c\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u8bed\u4e49\u5b8c\u5168\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7c7b\u578b\u3002\u6b64\u5916\uff0c\u5b83\u7684\u5b9e\u7528\u6027\u6709Parameters\uff0cPayload\u548cMultipart\u662f\u53ef\u4ea4\u6362\u7684\uff0c\u56e0\u4e3a\u6709\u65f6\u7684API\u6ca1\u6709\u4e25\u683c\u533a\u5206\u5b83\u4eec\u3002<\/p>\n<p>3\u3001\u4f1a\u8bdd\u5bf9\u8c61 \uff08Session Objects\uff09<br \/>\n\u5728\u5f15\u64ce\u76d6\u4e0b\uff0cSession\u5728\u6267\u884c\u8bf7\u6c42\u4e4b\u524d\uff0c\u6240\u6709\u5bf9\u4e3bAPI\u7684\u8c03\u7528\u90fd\u4f1a\u4fee\u6539\u540d\u4e3aa\u7684\u5bf9\u8c61\u3002\u8fd9\u662f\u56fe\u4e66\u9986\u552f\u4e00\u771f\u6b63\u6709\u72b6\u6001\u7684\u4e00\u90e8\u5206\uff0c\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\uff0c\u6ca1\u6709\u5fc5\u8981Session\u76f4\u63a5\u91c7\u53d6\u884c\u52a8\uff0c\u5b81\u613f\u8ba9\u56fe\u4e66\u9986\u4e3a\u4f60\u5904\u7406\u3002<br \/>\n\u4f46\u662f\uff0c\u5982\u679c\u6301\u6709\u72b6\u6001\u6709\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528Session\uff1a<\/p>\n<p>auto url = cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;};<br \/>\nauto parameters = cpr::Parameters{{&#8220;hello&#8221;, &#8220;world&#8221;}};<br \/>\ncpr::Session session;<br \/>\nsession.SetUrl(url);<br \/>\nsession.SetParameters(parameters);<\/p>\n<p>auto r = session.Get(); \/\/ Equivalent to cpr::Get(url, parameters);<br \/>\nstd::cout &lt;&lt; r.url &lt;&lt; std::endl \/\/ Prints http:\/\/www.httpbin.org\/get?hello=world<\/p>\n<p>auto new_parameters = cpr::Parameters{{&#8220;key&#8221;, &#8220;value&#8221;}};<br \/>\nsession.SetParameters(new_parameters);<\/p>\n<p>auto new_r = session.Get(); \/\/ Equivalent to cpr::Get(url, new_parameters);<br \/>\nstd::cout &lt;&lt; new_r.url &lt;&lt; std::endl \/\/ Prints http:\/\/www.httpbin.org\/get?key=value<\/p>\n<p>Session\u5b9e\u9645\u4e0a\u516c\u5f00\u4e86\u4e24\u4e2a\u4e0d\u540c\u7684\u754c\u9762\u6765\u8bbe\u7f6e\u76f8\u540c\u7684\u9009\u9879\u3002\u5982\u679c\u4f60\u60f3\u8981\uff0c\u4f60\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u800c\u4e0d\u662f\u4e0a\u8ff0\uff1a<\/p>\n<p>auto url = cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;};<br \/>\nauto parameters = cpr::Parameters{{&#8220;hello&#8221;, &#8220;world&#8221;}};<br \/>\ncpr::Session session;<br \/>\nsession.SetOption(url);<br \/>\nsession.SetOption(parameters);<br \/>\nauto r = session.Get();<\/p>\n<p>\u8fd9\u5f88\u91cd\u8981\uff0c\u6240\u4ee5\u5b83\u5f3a\u8c03\uff1a\u5bf9\u4e8e\u6bcf\u4e2a\u914d\u7f6e\u9009\u9879\uff08\u5982Url\uff0cParameters\uff09\uff0c\u90fd\u6709\u76f8\u5e94\u7684\u65b9\u6cd5Set\u548caSetOption()\u3002\u7b2c\u4e8c\u4e2a\u63a5\u53e3\u662f\u4e3a\u4e86\u4fc3\u8fdb\u6a21\u677f\u5143\u7f16\u7a0b\u9b54\u672f\uff0c\u8ba9API\u66b4\u9732\u65e0\u5e8f\u65b9\u6cd5\u3002<br \/>\n\u6240\u6709\u8fd9\u4e00\u5207\u7684\u5173\u952e\u5b9e\u9645\u4e0a\u5c31\u662flibcurl\u7684\u8bbe\u8ba1\u65b9\u5f0f\u3002\u5b83\u4f7f\u7528\u57fa\u4e8e\u7b56\u7565\u7684\u8bbe\u8ba1\uff0c\u4f9d\u8d56\u4e8e\u914d\u7f6e\u5355\u4e2a\u5e93\u5bf9\u8c61\uff08curl\u53e5\u67c4\uff09\u3002\u914d\u7f6e\u5230\u8be5\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u9009\u9879\u90fd\u4ee5\u5927\u90e8\u5206\u6b63\u4ea4\u7684\u65b9\u5f0f\u66f4\u6539\u5176\u884c\u4e3a\u3002<br \/>\nSession\u5229\u7528\u5b83\u5e76\u516c\u5f00\u4e00\u4e2a\u66f4\u73b0\u4ee3\u5316\u7684\u754c\u9762\uff0c\u514d\u9664\u4e86libcurl\u5de8\u5927\u7684\u6d6a\u8d39\u3002\u4e86\u89e3\u57fa\u4e8e\u7b56\u7565\u7684libcurl\u8bbe\u8ba1\u5bf9\u4e8e\u7406\u89e3Session\u5bf9\u8c61\u7684\u884c\u4e3a\u65b9\u5f0f\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<p>4\u3001\u5f02\u6b65\u8bf7\u6c42 \uff08Asynchronous Requests\uff09<br \/>\n\u5236\u4f5c\u5f02\u6b65\u8bf7\u6c42\u4f7f\u7528\u7c7b\u4f3c\u4f46\u72ec\u7acb\u7684\u63a5\u53e3\uff1a<\/p>\n<p>auto fr = cpr::GetAsync(cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<br \/>\n\/\/ Sometime later<br \/>\nauto r = fr.get(); \/\/ This blocks until the request is complete<br \/>\nstd::cout &lt;&lt; r.text &lt;&lt; std::endl;<\/p>\n<p>\u7535\u8bdd\u662f\u9664\uff0c\u800c\u4e0d\u662f\u5176\u4ed6\u76f8\u540c\u7684Get\uff0c\u5b83\u662fGetAsync\u3002\u4e0ePOST\u8bf7\u6c42\u7c7b\u4f3c\uff0c\u60a8\u53ef\u4ee5\u8c03\u7528PostAsync\u3002\u5f02\u6b65\u8c03\u7528\u7684\u8fd4\u56de\u503c\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2astd::future&lt;Response&gt;\uff1a<\/p>\n<p>auto fr = cpr::GetAsync(cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<br \/>\nfr.wait() \/\/ This waits until the request is complete<br \/>\nauto r = fr.get(); \/\/ Since the request is complete, this returns immediately<br \/>\nstd::cout &lt;&lt; r.text &lt;&lt; std::endl;<\/p>\n<p>\u4f60\u751a\u81f3\u53ef\u4ee5\u628a\u4e00\u5806\u8bf7\u6c42\u653e\u5165\u4e00\u4e2astd\u5bb9\u5668\u4e2d\uff0c\u5e76\u5728\u4ee5\u540e\u4f7f\u7528\u5b83\u4eec\uff1a<\/p>\n<p>auto container = std::vector&lt;std::future&lt;cpr::Response&gt;&gt;{};<br \/>\nauto url = cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;};<br \/>\nfor (int i = 0; i &lt; 10; ++i) {<br \/>\ncontainer.emplace_back(cpr::GetAsync(url, cpr::Parameters{{&#8220;i&#8221;, std::to_string(i)}}));<br \/>\n}<br \/>\n\/\/ Sometime later<br \/>\nfor (auto&amp; fr: container) {<br \/>\nauto r = fr.get();<br \/>\nstd::cout &lt;&lt; r.text &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>\u8fd9\u91cc\u8981\u505a\u7684\u4e00\u4e2a\u91cd\u8981\u7684\u6ce8\u610f\u4e8b\u9879\u662f\u4f20\u9012\u7ed9\u5f02\u6b65\u8c03\u7528\u7684\u53c2\u6570\u88ab\u590d\u5236\u3002\u5728\u5f15\u64ce\u76d6\u4e0b\uff0c\u901a\u8fc7\u5e93\u7684API\u7684\u5f02\u6b65\u8c03\u7528\u5b8c\u6210std::async\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e3a\u4e86\u5185\u5b58\u5b89\u5168\uff0c\u6240\u6709\u53c2\u6570\u90fd\u88ab\u590d\u5236\uff08\u6216\u8005\u5728\u4e34\u65f6\u79fb\u52a8\u65f6\uff09\uff0c\u56e0\u4e3a\u6ca1\u6709\u8bed\u6cd5\u7ea7\u522b\u4fdd\u8bc1\u53c2\u6570\u8d85\u51fa\u8bf7\u6c42\u7684\u8303\u56f4\u3002<br \/>\n\u53ef\u4ee5\u5f3a\u5236std::async\u4f7f\u7528\u6b64\u9ed8\u8ba4\u503c\uff0c\u4ee5\u4fbf\u53c2\u6570\u4f20\u9012\u53c2\u6570\u800c\u4e0d\u662f\u503c\u3002\u7136\u800c\uff0ccpr::Async\u76ee\u524d\u4e0d\u652f\u6301\u5f3a\u5236\u4f20\u9012\uff0c\u5c3d\u7ba1\u8fd9\u662f\u4e3a\u5c06\u6765\u7684\u7248\u672c\u8ba1\u5212\u7684\u3002<\/p>\n<p>5\u3001\u5f02\u6b65\u56de\u8c03\uff08Asynchronous Callbacks\uff09<br \/>\nC ++\u8bf7\u6c42\u8fd8\u652f\u6301\u5f02\u6b65\u8bf7\u6c42\u7684\u56de\u8c03\u63a5\u53e3\u3002\u4f7f\u7528\u56de\u8c03\u63a5\u53e3\uff0c\u4f20\u9012\u4e00\u4e2a\u4eff\u51fd\u6570\uff08lambda\uff0c\u51fd\u6570\u6307\u9488\u7b49\uff09\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u7136\u540e\u4f20\u5165\u5176\u4ed6\u901a\u5e38\u5728\u963b\u585e\u8bf7\u6c42\u4e2d\u9009\u62e9\u7684\u9009\u9879\u3002\u51fd\u5b50\u9700\u8981\u6709\u4e00\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2aResponse\u5bf9\u8c61 &#8211; \u5f53\u8bf7\u6c42\u5b8c\u6210\u5e76\u4e14\u51fd\u6570\u4f53\u6267\u884c\u65f6\uff0c\u8fd9\u4e2a\u54cd\u5e94\u88ab\u586b\u5145\u3002<br \/>\n\u8fd9\u91cc\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<p>auto future_text = cpr::GetCallback([](cpr::Response r) {<br \/>\nreturn r.text;<br \/>\n}, cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<br \/>\n\/\/ Sometime later<br \/>\nif (future_text.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {<br \/>\nstd::cout &lt;&lt; future_text.get() &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>\u8fd9\u91cc\u6709\u51e0\u4e2a\u5173\u952e\u7279\u6027\u9700\u8981\u6307\u51fa\uff1a<br \/>\n1. \u8fd4\u56de\u503c\u662fa std::string\u3002\u8fd9\u4e0d\u662f\u786c\u7f16\u7801\u7684 &#8211; \u56de\u8c03\u53ef\u4ee5\u81ea\u7531\u8fd4\u56de\u4efb\u4f55\u503c\uff01\u5f53\u9700\u8981\u83b7\u53d6\u8be5\u503c\u65f6\uff0c\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u5b8c\u6210\uff0c\u7136\u540e\u7b80\u5355\u5730.get()\u8c03\u7528\u5c06\u6765\u7684\u503c\u6765\u83b7\u53d6\u6b63\u786e\u7684\u503c\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u56de\u8c03\u754c\u9762\u975e\u5e38\u7b80\u5355\uff0c\u901a\u7528\u4e14\u9ad8\u6548\uff01<br \/>\n2. lambda\u6355\u83b7\u662f\u7a7a\u7684\uff0c\u4f46\u7edd\u5bf9\u4e0d\u9700\u8981\u3002\u4efb\u4f55\u53ef\u4ee5\u5728lambda\u4e2d\u6355\u83b7\u7684\u4e1c\u897f\u901a\u5e38\u90fd\u53ef\u4ee5\u5728\u4f20\u5165\u56de\u8c03\u63a5\u53e3\u7684lambda\u4e2d\u6355\u83b7\u3002\u8fd9\u79cd\u989d\u5916\u7684\u7075\u6d3b\u6027\u5411\u91cf\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\uff0c\u5c3d\u7ba1\u4efb\u4f55\u5177\u6709Response\u53c2\u6570\u7684\u51fd\u5b50\u90fd\u53ef\u4ee5\u7f16\u8bd1\u548c\u5de5\u4f5c\u3002<br \/>\n\u53e6\u5916\uff0c\u4f60\u53ef\u4ee5Response\u7528const Response&amp;\u53c2\u6570\u800c\u4e0d\u662f\u7b80\u5355\u5730\u5f3a\u5236\u6267\u884c\u4e0d\u53d8\u6027Response\u3002<\/p>\n<p>6\u3001\u8bbe\u7f6e\u8d85\u65f6 \uff08Setting Timeout\uff09<br \/>\n\u5982\u679c\u60a8\u6709\u4e25\u683c\u7684\u65f6\u95f4\u8981\u6c42\uff0c\u53ef\u4ee5\u4e3a\u60a8\u7684\u8bf7\u6c42\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff1a<\/p>\n<p>#include &lt;assert.h&gt;<\/p>\n<p>auto r = cpr::Get(cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;},<br \/>\ncpr::Timeout{1000}); \/\/ Let&#8217;s hope we aren&#8217;t using Time Warner Cable<br \/>\nassert(r.elapsed &lt;= 1); \/\/ Less than one second should have elapsed<\/p>\n<p>\u8bbe\u7f6eTimeout\u9009\u9879\u53ef\u8bbe\u7f6e\u4f20\u8f93\u64cd\u4f5c\u53ef\u4ee5\u627f\u53d7\u7684\u6700\u5927\u65f6\u95f4\u3002\u7531\u4e8eC ++\u8bf7\u6c42\u662f\u5efa\u7acb\u5728libcurl\u7684\u57fa\u7840\u4e0a\u7684\uff0c\u56e0\u6b64\u4e86\u89e3Timeout\u5bf9\u8bf7\u6c42\u505a\u4e86\u4ec0\u4e48\u8bbe\u7f6e\u5f88\u91cd\u8981\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\u66f4\u591a\u5173\u4e8e\u7279\u5b9alibcurl\u9009\u9879\u7684\u4fe1\u606f\u3002<\/p>\n<p>7\u3001\u4f7f\u7528\u4ee3\u7406 \uff08Using Proxies\uff09<br \/>\nProxies\uff0c\u5c31\u50cfParameters\u662f\u5730\u56fe\u5f0f\u7684\u5bf9\u8c61\u3002\u8bbe\u7f6e\u4e00\u4e2a\u5f88\u5bb9\u6613\uff1a<\/p>\n<p>auto r = cpr::Get(cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;},<br \/>\ncpr::Proxies{{&#8220;http&#8221;, &#8220;http:\/\/www.fakeproxy.com&#8221;}});<br \/>\nstd::cout &lt;&lt; r.url &lt;&lt; std::endl; \/\/ Prints http:\/\/www.httpbin.org\/get, not the proxy url<\/p>\n<p>\u5b83\u770b\u8d77\u6765\u5e76\u4e0dProxies\u50cf\u4e00\u5f20\u5730\u56fe\u90a3\u6837\u6709\u7528\uff0c\u4f46\u5728\u4f7f\u7528Session\u5b83\u65f6\u66f4\u660e\u663e\uff1a<\/p>\n<p>cpr::Session session;<br \/>\nsession.SetProxies({{&#8220;http&#8221;, &#8220;http:\/\/www.fakeproxy.com&#8221;},<br \/>\n{&#8220;https&#8221;, &#8220;http:\/\/www.anotherproxy.com&#8221;}})<br \/>\nsession.SetUrl(&#8220;http:\/\/www.httpbin.org\/get&#8221;);<br \/>\n{<br \/>\nauto r = session.Get();<br \/>\nstd::cout &lt;&lt; r.url &lt;&lt; std::endl; \/\/ Prints http:\/\/wcww.httpbin.org\/get after going<br \/>\n\/\/ through http:\/\/www.fakeproxy.com<br \/>\n}<br \/>\nsession.SetUrl(&#8220;https:\/\/www.httpbin.org\/get&#8221;);<br \/>\n{<br \/>\nauto r = session.Get();<br \/>\nstd::cout &lt;&lt; r.url &lt;&lt; std::endl; \/\/ Prints https:\/\/www.httpbin.org\/get after going<br \/>\n\/\/ through http:\/\/www.anotherproxy.com<br \/>\n}<\/p>\n<p>\u8bbe\u7f6eProxies\u4e00\u4e2aSession\u53ef\u4ee5\u8ba9\u4f60\u901a\u8fc7\u4f7f\u7528\u4e0d\u540c\u7684\u4ee3\u7406\u4e0d\u540c\u534f\u8bae\u667a\u80fd\u8def\u7531\u8bf7\u6c42\uff0c\u800c\u65e0\u9700respecify\u4ec0\u4e48\uff0c\u4f46\u8be5\u8bf7\u6c42Url\u3002<\/p>\n<p>8\u3001\u53d1\u9001Cookies \uff08SendingCookies\uff09<br \/>\n\u4e4b\u524d\u60a8\u770b\u5230\u4e86\u5982\u4f55\u4ece\u8bf7\u6c42\u4e2d\u83b7\u53d6cookie\uff1a<\/p>\n<p>auto r = cpr::Get(cpr::Url{&#8220;http:\/\/www.httpbin.org\/cookies\/set?cookies=yummy&#8221;});<br \/>\nstd::cout &lt;&lt; r.cookies[&#8220;cookies&#8221;] &lt;&lt; std::endl; \/\/ Prints yummy<br \/>\nstd::cout &lt;&lt; r.cookies[&#8220;Cookies&#8221;] &lt;&lt; std::endl; \/\/ Prints nothing<\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u53d1\u56deCookie\uff1a<\/p>\n<p>auto r = cpr::Get(cpr::Url{&#8220;http:\/\/www.httpbin.org\/cookies\/set?cookies=yummy&#8221;});<br \/>\nauto another_r = cpr::Get(cpr::Url{&#8220;http:\/\/www.httpbin.org\/cookies&#8221;}, r.cookies);<br \/>\nstd::cout &lt;&lt; another_r.text &lt;&lt; std::endl;<\/p>\n<p>\/*<br \/>\n* {<br \/>\n* &#8220;cookies&#8221;: {<br \/>\n* &#8220;cookie&#8221;: &#8220;yummy&#8221;<br \/>\n* }<br \/>\n* }<br \/>\n*\/<\/p>\n<p>\u8fd9\u662f\u7279\u522b\u6709\u7528\u7684\uff0c\u56e0\u4e3aCookies\u901a\u5e38\u4ece\u670d\u52a1\u5668\u5230\u5ba2\u6237\u7aef\u5e76\u8fd4\u56de\u5230\u670d\u52a1\u5668\u3002\u8bbe\u7f6e\u65b0\u7684Cookies\u5e94\u8be5\u4e0d\u4f1a\u4ee4\u4eba\u60ca\u8bb6\uff1a<\/p>\n<p>auto r = cpr::Get(cpr::Url{&#8220;http:\/\/www.httpbin.org\/cookies&#8221;},<br \/>\ncpr::Cookies{{&#8220;ice cream&#8221;, &#8220;is delicious&#8221;}});<br \/>\nstd::cout &lt;&lt; r.text &lt;&lt; std::endl;<\/p>\n<p>\/*<br \/>\n* {<br \/>\n* &#8220;cookies&#8221;: {<br \/>\n* &#8220;ice%20cream&#8221;: &#8220;is%20delicious&#8221;<br \/>\n* }<br \/>\n* }<br \/>\n*\/<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u6309\u7167RFC 2965\u7684\u8981\u6c42\uff0ccookie\u662f\u5982\u4f55\u4f7f\u7528url\u7f16\u7801\u6a21\u5f0f\u8fdb\u884c\u7f16\u7801\u7684\u3002\u9664\u4e86\u8fd9\u4e2a\u602a\u7656\uff0c\u4f7f\u7528Cookies\u76f8\u5f53\u7b80\u5355\uff0c\u53ea\u662f\u5de5\u4f5c\u3002<\/p>\n<p>9\u3001PUT\u548cPATCH\u8bf7\u6c42\uff08PUT and PATCH Requests\uff09<br \/>\nPUT\u548cPATCH\u8bf7\u6c42\u4e0ePOST\u8bf7\u6c42\u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\uff0c\u552f\u4e00\u7684\u4fee\u6539\u662f\u6307\u5b9a\u7684HTTP\u65b9\u6cd5\u662f\u201dPUT\u201d\u6216\u201dPATCH\u201d\u4e0d\u662f\u201dPOST\u201d\u3002\u5f53\u4f60\u8c03\u7528\u7684API\u7684\u8bed\u4e49\u5b9e\u73b0\u8fd9\u4e9b\u8bf7\u6c42\u7684\u7279\u6b8a\u884c\u4e3a\u65f6\u4f7f\u7528\u5b83\uff1a<\/p>\n<p>#include &lt;assert.h&gt;<\/p>\n<p>\/\/ We can&#8217;t POST to the &#8220;\/put&#8221; endpoint so the status code is rightly 405<br \/>\nassert(cpr::Post(cpr::Url{&#8220;http:\/\/www.httpbin.org\/put&#8221;},<br \/>\ncpr::Payload{{&#8220;key&#8221;, &#8220;value&#8221;}}).status_code == 405);<\/p>\n<p>\/\/ On the other hand, this works just fine<br \/>\nauto r = cpr::Put(cpr::Url{&#8220;http:\/\/www.httpbin.org\/put&#8221;},<br \/>\ncpr::Payload{{&#8220;key&#8221;, &#8220;value&#8221;}});<br \/>\nstd::cout &lt;&lt; r.text &lt;&lt; std::endl;<\/p>\n<p>\/*<br \/>\n* {<br \/>\n* &#8220;args&#8221;: {},<br \/>\n* &#8220;data&#8221;: &#8220;&#8221;,<br \/>\n* &#8220;files&#8221;: {},<br \/>\n* &#8220;form&#8221;: {<br \/>\n* &#8220;key&#8221;: &#8220;value&#8221;<br \/>\n* },<br \/>\n* &#8220;headers&#8221;: {<br \/>\n* ..<br \/>\n* &#8220;Content-Type&#8221;: &#8220;application\/x-www-form-urlencoded&#8221;,<br \/>\n* ..<br \/>\n* },<br \/>\n* &#8220;json&#8221;: null,<br \/>\n* &#8220;url&#8221;: &#8220;https:\/\/httpbin.org\/put&#8221;<br \/>\n* }<br \/>\n*\/<\/p>\n<p>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cPUT\u7528\u4e8e\u4f7f\u7528\u65b0\u5bf9\u8c61\u66f4\u65b0\u73b0\u6709\u5bf9\u8c61\u3002\u5f53\u7136\uff0c\u8fd9\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u4efb\u4f55\u7279\u5b9a\u7684API\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528PUT\u8bed\u4e49\uff0c\u6240\u4ee5\u53ea\u6709\u5728\u5b83\u6709\u610f\u4e49\u65f6\u624d\u4f7f\u7528\u5b83\u3002\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8bPATCH\u8bf7\u6c42\uff0c\u5b83\u57fa\u672c\u76f8\u540c\uff1a<\/p>\n<p>#include &lt;assert.h&gt;<\/p>\n<p>\/\/ We can&#8217;t POST or PUT to the &#8220;\/patch&#8221; endpoint so the status code is rightly 405<br \/>\nassert(cpr::Post(cpr::Url{&#8220;http:\/\/www.httpbin.org\/patch&#8221;},<br \/>\ncpr::Payload{{&#8220;key&#8221;, &#8220;value&#8221;}}).status_code == 405);<br \/>\nassert(cpr::Put(cpr::Url{&#8220;http:\/\/www.httpbin.org\/patch&#8221;},<br \/>\ncpr::Payload{{&#8220;key&#8221;, &#8220;value&#8221;}}).status_code == 405);<\/p>\n<p>\/\/ On the other hand, this works just fine<br \/>\nauto r = cpr::Patch(cpr::Url{&#8220;http:\/\/www.httpbin.org\/patch&#8221;},<br \/>\ncpr::Payload{{&#8220;key&#8221;, &#8220;value&#8221;}});<br \/>\nstd::cout &lt;&lt; r.text &lt;&lt; std::endl;<\/p>\n<p>\/*<br \/>\n* {<br \/>\n* &#8220;args&#8221;: {},<br \/>\n* &#8220;data&#8221;: &#8220;&#8221;,<br \/>\n* &#8220;files&#8221;: {},<br \/>\n* &#8220;form&#8221;: {<br \/>\n* &#8220;key&#8221;: &#8220;value&#8221;<br \/>\n* },<br \/>\n* &#8220;headers&#8221;: {<br \/>\n* ..<br \/>\n* &#8220;Content-Type&#8221;: &#8220;application\/x-www-form-urlencoded&#8221;,<br \/>\n* ..<br \/>\n* },<br \/>\n* &#8220;json&#8221;: null,<br \/>\n* &#8220;url&#8221;: &#8220;https:\/\/httpbin.org\/patch&#8221;<br \/>\n* }<br \/>\n*\/<\/p>\n<p>\u548cPUT\u4e00\u6837\uff0c\u53ea\u6709\u5f53\u60a8\u53d1\u9001\u8bf7\u6c42\u7684API\u652f\u6301\u8be5\u65b9\u6cd5\u65f6\uff0cPATCH\u624d\u6709\u6548\u3002<br \/>\n\u5176\u4ed6\u8bf7\u6c42\u65b9\u6cd5<br \/>\nC ++\u8bf7\u6c42\u8fd8\u652f\u6301DELETE\uff0cHEAD\u4ee5\u53caOPTIONS\u5728\u9884\u671f\u7684\u5f62\u5f0f\u7684\u65b9\u6cd5\uff1a<\/p>\n<p>\/\/ Regular, blocking modes<br \/>\nauto delete_response = cpr::Delete(cpr::Url{&#8220;http:\/\/www.httpbin.org\/delete&#8221;});<br \/>\nauto head_response = cpr::Head(cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<br \/>\nauto options_response = cpr::OPTIONS(cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<\/p>\n<p>\/\/ Asynchronous, future mode<br \/>\nauto async_delete_response = cpr::DeleteAsync(cpr::Url{&#8220;http:\/\/www.httpbin.org\/delete&#8221;});<br \/>\nauto async_head_response = cpr::HeadAsync(cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<br \/>\nauto async_options_response = cpr::OptionsAsync(cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<\/p>\n<p>\/\/ Asynchronous, callback mode<br \/>\nauto cb_delete_response = cpr::DeleteCallback([](cpr::Response r) {<br \/>\nreturn r.text;<br \/>\n}, cpr::Url{&#8220;http:\/\/www.httpbin.org\/delete&#8221;});<br \/>\nauto cb_head_response = cpr::HeadCallback([](cpr::Response r) {<br \/>\nreturn r.status_code;<br \/>\n}, cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<br \/>\nauto cb_options_response = cpr::OptionsCallback([](cpr::Response r) {<br \/>\nreturn r.status_code;<br \/>\n}, cpr::Url{&#8220;http:\/\/www.httpbin.org\/get&#8221;});<\/p>\n<p>\u76ee\u524d\uff0c\u201dPATCH\u201d\u4e0d\u662f\u4e00\u79cd\u5b9e\u65bd\u7684HTTP\u65b9\u6cd5\u3002\u5b83\u5f88\u5feb\u5c31\u4f1a\u6210\u7acb\uff0c\u5176\u673a\u5236\u5c06\u4e0e\u4e0a\u8ff0\u4f8b\u5b50\u76f8\u4e00\u81f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 \u539f\u521b\u738b\u9ad8_1990 \u6700\u540e\u53d1\u5e03\u4e8e<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-4219","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4219"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4219\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}