个性化阅读
专注于IT技术分析

11 cURL命令用法与实时示例

点击下载

本文概述

cURL是使用URL语法获取或发送数据的命令行工具。

如果你是开发人员或在支持人员中工作, 则必须了解cURL命令的使用以对Web应用程序进行故障排除。 cURL是跨平台实用程序, 意味着你可以在Windows, MAC和UNIX上使用。

以下是一些最常用的语法, 并带有示例以帮助你。

验证是否可以连接到URL

如果你正在UNIX系统上工作并尝试连接外部URL, 那么你要做的第一件事就是检查是否可以通过curl访问URL。

curl yoururl.com

它不会抛出任何输出。但是, 如果服务器无法连接, 则会出现错误, 例如无法解析主机。

[[email protected] tmp]# curl helloitdoesntexist.com
curl: (6) Could not resolve host: helloitdoesntexist.com; Unknown error
[[email protected] tmp]#

将URL / URI输出保存到文件

如果必须将URL或URI内容保存到特定文件, 则可以使用以下语法

curl https://yoururl.com > yoururl.html

例如:

[[email protected] 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
[[email protected] tmp]#

上面的示例会将所有内容从gf.dev保存到/tmp/gf.html

显示请求和响应头

如果你遇到问题并希望进行验证, 则可以获取预期的请求和响应标头。

curl -v yoururl.com

例如:

[[email protected] 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

例如:

[[email protected] 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

[[email protected] tmp]#

连接HTTPS / SSL URL并忽略任何SSL证书错误

当你尝试访问SSL / TLS证书安全的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。

在线CURL –一种轻巧的工具, 可在线获取URL, 并可以添加以下选项。

--connect-timeout
--cookie
--data
--header
--head
--location
--max-time
--proxy
--request
--user
--url
--user-agent

cURL命令行生成器–这是不同的。它可以帮助你构建curl命令, 在其中可以在漂亮的UI中输入信息, 在底部, 你将获得cURL命令。

curl builder

cURL是解决实时连接问题的有用工具, 希望以上内容对你有所帮助。如果你想了解更多信息, 那么我将推荐Linux Command Line Basics在线课程。

赞(0)
未经允许不得转载:srcmini » 11 cURL命令用法与实时示例

评论 抢沙发

评论前必须登录!