{"id":62,"date":"2021-08-31T08:35:36","date_gmt":"2021-08-31T00:35:36","guid":{"rendered":"https:\/\/blog.wnstudio.net\/?p=62"},"modified":"2021-10-22T21:10:35","modified_gmt":"2021-10-22T13:10:35","slug":"curl","status":"publish","type":"post","link":"https:\/\/blog.wnstudio.net\/post\/curl\/","title":{"rendered":"curl"},"content":{"rendered":"\n

curl \u662f\u4e00\u500b\u900f\u904e\u7db2\u8def\u5354\u5b9a\u50b3\u8f38\u8cc7\u6599\u7684\u6307\u4ee4\uff0c\u7528\u5f88\u767d\u8a71\u7684\u8aaa\u6cd5\u5c31\u662f\u8b93\u4f60\u300c\u9023\u7dda\u5230\u7db2\u7ad9\u300d\u3002<\/p>\n\n\n\n

\u57fa\u672c\u4f7f\u7528<\/h2>\n\n\n\n
curl http:\/\/www.example.com\n<\/code><\/pre>\n\n\n\n

\u9019\u6a23\u6703\u5c07 response body print \u51fa\u4f86\uff0c\u70ba\u4e86\u8981\u5feb\u901f\u9a57\u8b49\u7db2\u7ad9\u7684\u56de\u61c9\u662f\u5426\u6b63\u5e38\uff0c\u6211\u6bd4\u8f03\u5e38\u7528\u7684\u5f62\u5f0f\u662f<\/p>\n\n\n\n

curl -iL http:\/\/www.example.com<\/code><\/pre>\n\n\n\n

-i \u4ee3\u8868\u986f\u793a header + body \uff0c\u5982\u679c\u53ea\u8981 header \u7684\u8a71\uff0c\u53ef\u4ee5\u4f7f\u7528\u5927\u5beb\u7684 I \u3002<\/p>\n\n\n\n

-L \u5247\u4ee3\u8868\u8ddf\u96a8\u7db2\u5740\u7684 redirect \u898f\u5247\u3002\u4f8b\u5982\u8aaa\uff0c\u73fe\u5728\u7684\u4e3b\u6d41\u7db2\u7ad9\u7528 HTTP \u9023\u7dda\u6703\u81ea\u52d5\u8f49\u6210 HTTPs \u5c31\u53ef\u4ee5\u900f\u904e curl \u4f86\u89c0\u5bdf\u3002<\/p>\n\n\n\n

curl -IL http:\/\/google.com\n\n---\n\nHTTP\/1.1 301 Moved Permanently\nLocation: http:\/\/www.google.com\/\nContent-Type: text\/html; charset=UTF-8\nDate: Mon, 30 Aug 2021 23:42:39 GMT\nExpires: Wed, 29 Sep 2021 23:42:39 GMT\nCache-Control: public, max-age=2592000\nServer: gws\nContent-Length: 219\nX-XSS-Protection: 0\nX-Frame-Options: SAMEORIGIN\n\nHTTP\/1.1 200 OK\nContent-Type: text\/html; charset=ISO-8859-1\nP3P: CP=\"This is not a P3P policy! See g.co\/p3phelp for more info.\"\nDate: Mon, 30 Aug 2021 23:42:39 GMT\nServer: gws\nX-XSS-Protection: 0\nX-Frame-Options: SAMEORIGIN\nTransfer-Encoding: chunked\nExpires: Mon, 30 Aug 2021 23:42:39 GMT\nCache-Control: private<\/code><\/pre>\n\n\n\n

\u4ee5 google \u70ba\u4f8b\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u7b2c\u4e00\u500b request \u7684 header \u662f 301 Move Permanently\uff0c\u4e14\u5305\u542b\u4e86 Location \uff0c\u6709\u52a0\u4e0a -L \u53c3\u6578\u7684\u8a71 curl \u5c31\u6703\u8ddf\u96a8 redirect \u7684location \u7e7c\u7e8c request\u3002<\/p>\n\n\n\n

\u8f38\u51fa\u66f4\u591a\u8a0a\u606f<\/h2>\n\n\n\n

\u53ef\u4ee5\u7528 -v \u6216 –verbose \u8b93 curl \u8f38\u51fa\u66f4\u8a73\u7d30\u7684\u8a0a\u606f\uff0c\u6211\u90fd\u62ff\u4f86\u770b\u5b83\u5be6\u969b\u4e0a\u9023\u7dda\u5230\u54ea\u4e00\u53f0\u4e3b\u6a5f<\/p>\n\n\n\n

curl -v https:\/\/example.com\n\ncurl --verbose https:\/\/example.com\n\n~ \u276f curl -v https:\/\/example.com                                                                            at 08:04:25\n*   Trying 93.184.216.34...\n* TCP_NODELAY set\n* Connected to example.com (93.184.216.34) port 443 (#0)\n* ALPN, offering h2\n* ALPN, offering http\/1.1\n* successfully set certificate verify locations:\n*   CAfile: \/etc\/ssl\/cert.pem\n  CApath: none\n* TLSv1.2 (OUT), TLS handshake, Client hello (1):\n* TLSv1.2 (IN), TLS handshake, Server hello (2):\n\n\u2026\u4e0b\u7565<\/code><\/pre>\n\n\n\n

\u6307\u5b9a\u8981\u9023\u7dda\u7684\u4e3b\u6a5f<\/h2>\n\n\n\n

curl \u9810\u8a2d\u6703\u4f7f\u7528 DNS resolve \u4f86\u6c7a\u5b9a\u8981\u4f7f\u7528\u54ea\u4e00\u53f0\u4e3b\u6a5f\u3002<\/p>\n\n\n\n

CDN \u7684\u958b\u767c\u4e0a\uff0c\u5e38\u6703\u9700\u8981\u5c07 request \u9001\u5230\u7279\u5b9a\u7684\u4e3b\u6a5f\uff0c\u53ef\u4ee5\u7528\u4fee\u6539 host \u6a94\u7684\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u7528\u53c3\u6578 –resolve \u76f4\u63a5\u5c07 DNS resolve \u52a0\u9032\u53c3\u6578\uff0c\u65b9\u4fbf\u7a0b\u5f0f\u5316\u3002<\/p>\n\n\n\n

curl --resolve google.com:443:127.0.0.1 --resolve google.com:80:127.0.0.1 https:\/\/google.com -v\n\n* Added google.com:443:127.0.0.1 to DNS cache\n* Added google.com:80:127.0.0.1 to DNS cache\n* Hostname google.com was found in DNS cache\n*   Trying 127.0.0.1...\n* TCP_NODELAY set\n* Connection failed\n* connect to 127.0.0.1 port 443 failed: Connection refused\n* Failed to connect to google.com port 443: Connection refused\n* Closing connection 0<\/code><\/pre>\n\n\n\n

\u9023\u7dda\u5230 google.com \uff0c80 port \u53ca 443 port \u90fd\u9023\u7dda\u5230 127.0.0.1 \u3002\u52a0\u4e0a -v \u5c31\u53ef\u4ee5\u770b\u5230 curl \u78ba\u5be6\u5c07 request \u9001\u5230 127.0.0.1\u3002<\/p>\n\n\n\n

\u7528\u6307\u5b9a Header \u7684\u65b9\u5f0f\u4e5f\u53ef\u4ee5\u9054\u6210\u985e\u4f3c\u7684\u6548\u679c\uff1a<\/p>\n\n\n\n

curl -H \"Host: www.google.com\" http:\/\/127.0.0.1\/path\/to\/resource<\/code><\/pre>\n\n\n\n

\u4f46\u4f7f\u7528\u6642\u8981\u6ce8\u610f\u9019\u6a23\u8ddf client \u5be6\u969b\u9001\u51fa\u7684 request \u662f\u4e0d\u5b8c\u5168\u76f8\u540c\u7684\uff0c\u5982\u679c\u7528\u7684\u662f HTTPs \uff0c certificate \u5c31\u4e0d\u80fd\u9806\u5229\u751f\u6548\uff0c\u53ef\u4ee5\u914d\u5408 -k \u4f7f\u7528\u3002<\/p>\n\n\n\n

\u6bd4\u8f03\u9069\u5408\u5feb\u901f\u9a57\u8b49\uff0c\u8981\u5b8c\u6574\u7684\u8a71\u9084\u662f\u7528 –resolve \u70ba\u4f73\u3002<\/p>\n\n\n\n

\u5ffd\u7565 SSL \u6191\u8b49<\/h2>\n\n\n\n

\u5982\u679c\u8981\u9023\u7dda\u7684\u7db2\u7ad9\u6191\u8b49\u904e\u671f\uff0c\u6216\u662f\u56e0\u70ba\u5176\u4ed6\u539f\u56e0\u4f60\u4e26\u4e0d\u5728\u4e4e\u6191\u8b49\u662f\u5426\u6709\u6548\uff0c\u53ef\u4ee5\u4f7f\u7528 -k (\u6216\u662f\u66f4\u6709\u8868\u9054\u529b\u7684 –insecure )<\/p>\n\n\n\n

# \u8a66\u8457 curl \u4e00\u500b\u6191\u8b49\u904e\u671f\u7684\u7db2\u7ad9\uff0c\u6703\u7522\u751f\u932f\u8aa4\ncurl https:\/\/certificate.expired.net\/\n\ncurl: (60) SSL certificate problem: certificate has expired\nMore details here: https:\/\/curl.haxx.se\/docs\/sslcerts.html\n\ncurl failed to verify the legitimacy of the server and therefore could not\nestablish a secure connection to it. To learn more about this situation and\nhow to fix it, please visit the web page mentioned above.\n\n# \u8996\u9700\u6c42\uff0c\u4f60\u53ef\u80fd\u4e26\u4e0d\u5728\u4e4e\u6191\u8b49\u6642\uff0c\u53ef\u4ee5\u7565\u904e\u5b83\ncurl -k https:\/\/certificate.expired.net\/\n<\/a>\ncurl --insecure https:\/\/certificate.expired.net\/<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

<\/p>\n\n\n\n

\u4f7f\u7528\u4e0d\u540c\u7684 Request Method<\/h2>\n\n\n\n

curl \u9810\u8a2d\u4f7f\u7528 GET \uff0c\u6709\u6642\u5019\u53ef\u80fd\u6703\u9700\u8981\u4ee5 POST \u6216\u5176\u4ed6\u4e0d\u540c\u7684 method \u767c\u9001 request<\/p>\n\n\n\n

curl -X POST https:\/\/example.com<\/code><\/pre>\n\n\n\n

\u986f\u793a\u82b1\u8cbb\u6642\u9593<\/h2>\n\n\n\n

\u5982\u679c\u9700\u8981\u6642\u9593\u8cc7\u8a0a\uff0c\u53ef\u4ee5\u4f7f\u7528 -w<\/p>\n\n\n\n

curl -I example.com -w %{time_connect}-%\"{time_starttransfer}-%{time_total}\\n\"<\/code><\/pre>\n\n\n\n

man curl \u53ef\u4ee5\u770b\u5230\u66f4\u591a\u80fd\u7528\u7684 variables<\/p>\n\n\n\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

curl\u6307\u4ee4 curl\u600e\u9ebc\u7528<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.wnstudio.net\/wp-json\/wp\/v2\/posts\/62"}],"collection":[{"href":"https:\/\/blog.wnstudio.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wnstudio.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wnstudio.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wnstudio.net\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":2,"href":"https:\/\/blog.wnstudio.net\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/blog.wnstudio.net\/wp-json\/wp\/v2\/posts\/62\/revisions\/94"}],"wp:attachment":[{"href":"https:\/\/blog.wnstudio.net\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wnstudio.net\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wnstudio.net\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}