10 cURL 命令用法与实时示例
已发表: 2015-02-12cURL 是一个使用 URL 语法获取或发送数据的命令行工具。
如果您是开发人员或支持职能部门,则必须了解cURL 命令的用法以对 Web 应用程序进行故障排除。 cURL 是一个跨平台实用程序,意味着您可以在 Windows、MAC 和 UNIX 上使用。
以下是一些最常用的语法,并带有一个示例来帮助您。
验证是否可以连接到 URL
如果您在 UNIX 系统上工作并尝试连接外部 URL,那么您要做的第一件事就是检查是否可以通过 curl 访问该 URL。
curl yoururl.com
它不会抛出任何输出。 但是,如果服务器无法连接,则会出现无法解析主机等错误。
[root@gf-lab tmp]# curl helloitdoesntexist.com curl: (6) Could not resolve host: helloitdoesntexist.com; Unknown error [root@gf-lab tmp]#
将 URL/URI 输出保存到文件
如果必须将 URL 或 URI 内容保存到特定文件,可以使用以下语法
curl https://yoururl.com > yoururl.html
前任:
[root@gf-lab tmp]# curl https://gf.dev > /tmp/gfhtml % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 18557 0 18557 0 0 72565 0 --:--:-- --:--:-- --:--:-- 72772 [root@gf-lab tmp]#
上面的例子会将 gf.dev 中的所有内容保存到 /tmp/gf.html
显示请求和响应标头
如果您遇到问题并想要验证,您将获得预期的请求和响应标头。
curl -v yoururl.com
前任:
[root@gf-lab tmp]# curl -v https://geekflare.com * About to connect() to geekflare.com port 443 (#0) * Trying 104.25.134.107... * Connected to geekflare.com (104.25.134.107) port 443 (#0) * Initializing NSS with certpath: sql:/etc/pki/nssdb * CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none * SSL connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 * Server certificate: * subject: CN=ssl371609.cloudflaressl.com,OU=PositiveSSL Multi-Domain,OU=Domain Control Validated * start date: Nov 07 00:00:00 2019 GMT * expire date: May 15 23:59:59 2020 GMT * common name: ssl371609.cloudflaressl.com * issuer: CN=COMODO ECC Domain Validation Secure Server CA 2,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: geekflare.com > Accept: */* > < HTTP/1.1 200 OK < Date: Sat, 09 Nov 2019 19:41:37 GMT < Content-Type: text/html; charset=UTF-8 < Transfer-Encoding: chunked < Connection: keep-alive < Set-Cookie: __cfduid=d2ce6cd359ebc0b6eb5ff3a454ed042021573328497; expires=Sun, 08-Nov-20 19:41:37 GMT; path=/; domain=.geekflare.com; HttpOnly; Secure < Vary: Accept-Encoding < Link: <https://geekflare.com/wp-json/>; rel="https://api.w.org/" < Link: <https://geekflare.com/>; rel=shortlink < X-SRCache-Fetch-Status: HIT < X-SRCache-Store-Status: BYPASS < X-Frame-Options: SAMEORIGIN < X-Powered-By: EasyEngine v4.0.12 < Via: 1.1 google < CF-Cache-Status: DYNAMIC < Strict-Transport-Security: max-age=15552000; preload < X-Content-Type-Options: nosniff < Alt-Svc: h3-23=":443"; ma=86400 < Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" < Server: cloudflare < CF-RAY: 533243e4bcd4bbf4-LHR <
限速下载
如果您正在进行优化并想查看以特定速度下载需要多长时间,您可以:-
curl –-limit-rate 2000B
前任:
curl –-limit-rate 2000B https://gf.dev
使用代理连接
如果您在需要使用代理连接到外部世界的 DMZ 服务器上工作,这将非常方便。
curl --proxy yourproxy:port https://yoururl.com
带有注入标头的测试 URL
您可以通过在数据中插入标题来使用 curl 来测试或解决特定问题。 让我们看以下使用 Content-Type 请求的示例。

curl --header 'Content-Type: application/json' http://yoururl.com
通过上述操作,您要求 curl 在请求标头中将 Content-Type 作为 application/json 传递。
仅显示响应标头
如果您正在进行一些故障排除并快速想要检查响应标头,则可以使用以下语法。
curl --head http://yoururl.com
前任:
[root@gf-lab tmp]# curl --head https://chandan.io HTTP/1.1 200 OK Date: Sat, 09 Nov 2019 19:51:23 GMT Content-Type: text/html Connection: keep-alive Set-Cookie: __cfduid=d3cb2c7b8e566ad99c870b0af12b0f1eb1573329083; expires=Sun, 08-Nov-20 19:51:23 GMT; path=/; domain=.chandan.io; HttpOnly X-GUploader-UploadID: AEnB2Uo96JhvJmR2zYUL-Ndh2ta3UD_ykQAB5C7O8cjZQhCf-GxHQ0MsodSzRnl3guSN3ywAYNjtWcPXfwDXjLg3bQ-P5vQMOA Expires: Sat, 09 Nov 2019 20:51:23 GMT Cache-Control: public, max-age=3600 Last-Modified: Mon, 06 Aug 2018 10:45:47 GMT x-goog-generation: 1533552347482034 x-goog-metageneration: 1 x-goog-stored-content-encoding: identity x-goog-stored-content-length: 24620 x-goog-hash: crc32c=DpDPAQ== x-goog-hash: md5=cIP/3rusdUx12Zla1kf1yA== x-goog-storage-class: MULTI_REGIONAL Accept-Ranges: bytes CF-Cache-Status: DYNAMIC Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" Server: cloudflare CF-RAY: 53325234dc2fbb9a-LHR [root@gf-lab tmp]#
连接 HTTPS/SSL URL 并忽略任何 SSL 证书错误
当您尝试访问 SSL/TLS 证书安全 URL 时,如果该 URL 具有错误的证书或 CN 不匹配,那么您将收到以下错误。
curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.
好消息,您可以使用--insecure
标志指示 cURL 忽略证书错误。
curl --insecure https://yoururl.com
使用特定协议 (SSL/TLS) 连接
测试特定 URL 是否可以通过特定 SSL/TLS 协议握手非常方便。
使用 SSL v3 连接
curl --sslv3 https://yoururl.com
对于不同的 TLS 版本
curl --tlsv1 https://example.com curl --tlsv1.0 https://example.com curl --tlsv1.1 https://example.com curl --tlsv1.2 https://example.com curl --tlsv1.3 https://example.com
从 FTP 服务器下载文件
您也可以通过指定用户名和密码来使用 curl 下载文件。
curl -u user:password -O ftp://ftpurl/style.css
您始终可以使用带有任何语法的“ -v ”以详细模式打印。
使用主机头
当请求的内容仅在主机标头匹配时可用时,主机标头可用于通过 IP 测试目标 URL。 或者,如果您想使用负载平衡器 IP/URL 测试应用程序。
curl --header 'Host: targetapplication.com' https://192.0.0.1:8080/
在线使用 cURL 怎么样?
是的,使用以下工具可以做到这一点。 您可以远程执行 cURL。
Online CURL – 一个轻量级的在线获取 URL 的工具,并且可以添加以下选项。
--connect-timeout --cookie --data --header --head --location --max-time --proxy --request --user --url --user-agent
cURL 命令行生成器——这个是不同的。 它可以帮助您构建 curl 命令,您可以在其中在漂亮的 UI 中输入信息,在底部,您将获得 cURL 命令。

cURL 是解决实时连接问题的有用实用程序,希望以上内容对您有所帮助。 如果您想了解更多信息,那么我会推荐 Linux 命令行基础在线课程。