curl
curl:Command Line URL viewer
curl
是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在“标准输出”(stdout)上面。
-
curl --help
命令可查看帮助信息
一、查看网页源码
$ curl www.example.com
输出:
Example Domain
Example Domain
This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.
二、保存网页
-
-o
参数:把网页中的文件输出到本地一个文件(自定义文件名)中。等同于wget命令。 -
-O
参数:把网页中的文件输出到本地一个文件(将url的最后部分作为文件名)中。
1. -o
$ curl -o output.html www.example.com
// 保存为output.html
2. -O
$ curl -O https://www.example.com/foo/bar.html
// 保存为bar.html
:文件被保存在哪里?
- mac终端curl下载的文件在哪
三、不输出
-
-s
参数:不输出错误和进度信息 -
-S
参数:指定只输出错误信息,通常与-s
一起使用
-s
$ curl -s www.example.com
// 上面命令一旦发生错误,不会显示错误信息。
// 不发生错误的话,会正常显示运行结果。
例1:
image.png
例2:
-S
$ curl -S www.example.com
// 上面命令没有任何输出,除非发生错误。
四、显示头信息
-
-i
参数:可以显示http response 的头信息,连同网页代码一起。 -
-I
参数:则只显示http response 的头信息。 同--head
1. curl -i
$ curl -i www.sina.com
输出:
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 25 Aug 2020 10:23:22 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.sina.com.cn/
Expires: Tue, 25 Aug 2020 10:23:45 GMT
Cache-Control: max-age=120
X-Via-SSL: ssl.95.sinag1.qxg.lb.sinanode.com
Edge-Copy-Time: 1598351002111
Age: 97
Via: https/1.1 ctc.guangzhou.union.182 (ApacheTrafficServer/6.2.1 [cRs f ]), https/1.1 ctc.ningbo.union.47 (ApacheTrafficServer/6.2.1 [cRs f ])
X-Via-Edge: 15983510027946ce52478f0beee733509e3da
X-Cache: HIT.47
X-Via-CDN: f=edge,s=ctc.ningbo.union.70.nb.sinaedge.com,c=120.36.229.108;f=Edge,s=ctc.ningbo.union.47,c=115.238.190.70
301 Moved Permanently
301 Moved Permanently
nginx
2. curl -I
$ curl -I www.sina.com
// 或者
$ curl --head www.sina.com
输出:
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 26 Aug 2020 01:50:16 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.sina.com.cn/
Expires: Wed, 26 Aug 2020 01:51:03 GMT
Cache-Control: max-age=120
X-Via-SSL: ssl.95.sinag1.qxg.lb.sinanode.com
Edge-Copy-Time: 1598406615342
Age: 73
Via: https/1.1 ctc.guangzhou.union.182 (ApacheTrafficServer/6.2.1 [cRs f ]), https/1.1 ctc.ningbo.union.47 (ApacheTrafficServer/6.2.1 [cRs f ])
X-Via-Edge: 1598406616333dba8393bf0beee7326154d4c
X-Cache: HIT.47
X-Via-CDN: f=edge,s=ctc.ningbo.union.73.nb.sinaedge.com,c=59.57.168.219;f=Edge,s=ctc.ningbo.union.47,c=115.238.190.73
五、显示通信过程
-
-v
参数:可以显示一次http通信的整个过程,包括端口连接和http request头信息。 - 如果你觉得
-v
显示的信息还不够,可以使用-- trace [输出文件名]
命令或者--trace-ascii [输出文件名]
命令查看更详细的内容。
$ curl -v www.sina.com
// 或者(以下两个命令运行后请打开output.txt查看)
$ curl --trace output.txt www.sina.com (含16进制信息)
$ curl --trace-ascii output.txt www.sina.com (不含16进制信息)
输出
* Trying 115.238.190.240...
* TCP_NODELAY set
* Connected to www.sina.com (115.238.190.240) port 80 (#0)
> GET / HTTP/1.1
> Host: www.sina.com
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Wed, 26 Aug 2020 02:14:25 GMT
< Content-Type: text/html
< Content-Length: 178
< Connection: keep-alive
< Location: http://www.sina.com.cn/
< Expires: Wed, 26 Aug 2020 02:15:13 GMT
< Cache-Control: max-age=120
< X-Via-SSL: ssl.22.sinag1.qxg.lb.sinanode.com
< Edge-Copy-Time: 1598408064302
< Age: 72
< Via: https/1.1 ctc.guangzhou.union.182 (ApacheTrafficServer/6.2.1 [cRs f ]), https/1.1 ctc.ningbo.union.47 (ApacheTrafficServer/6.2.1 [cRs f ])
< X-Via-Edge: 15984080658086ce52478f0beee730184920e
< X-Cache: HIT.47
< X-Via-CDN: f=edge,s=ctc.ningbo.union.74.nb.sinaedge.com,c=120.36.229.108;f=Edge,s=ctc.ningbo.union.47,c=115.238.190.74
<
301 Moved Permanently
301 Moved Permanently
nginx
* Connection #0 to host www.sina.com left intact
* Closing connection 0
六、发送表单信息
- 发送表单信息有
get
和post
两种方法。 -
get
方法:只要把数据附在网址后面即可。 -
post
方法:必须把数据和网址分开,curl就要用到-data
参数。
1. GET方法
$ curl example.com/form.cgi?data=xxx
2. POST方法
$ curl -X POST --data "data=xxx" example.com/form.cgi
如果你的数据没有经过表单编码,还可以使用curl为你编码,参数为 --data-urlencode
$ curl -X POST --data-urlencode "date=April 1" example.com/form.cgi
参考资料
curl网站开发指南 - 阮一峰的网络日志
curl 的用法指南 - 阮一峰的网络日志