1. 概述
- cURL是一个利用URL语法在命令行下工作的文件传输工具,1997年首次发行。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称cURL为下载工具。cURL还包含了用于程序开发的libcurl。
2. 常见用法
1. 查看网页源码
- 在不加任何选项使用curl时,默认会发送GET请求来获取链接内容到标准输出。
curl www.sina.com
- 把这个网页保存下来,就可以使用
-o
参数。testsave为[文件名]。
curl -o testsave www.sina.com
2. 自动跳转
- 有的网站是自动跳转的,使用
-L
参数,curl就会跳转到新的网址。
curl -L www.sina.com
3. 显示头信息
-i
参数可以显示http response的头信息,连同网页代码一起。
curl -i www.sina.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Fri, 04 Sep 2020 09:14:50 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.sina.com.cn/
Expires: Fri, 04 Sep 2020 09:15:25 GMT
Cache-Control: max-age=120
X-Via-SSL: ssl.23.sinag1.qxg.lb.sinanode.com
Edge-Copy-Time: 1599210890645
Age: 85
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: 159921089062925588877f0beee7374f5da2e
X-Cache: MISS.MERGE.47
X-Via-CDN: f=edge,s=ctc.ningbo.union.74.nb.sinaedge.com,c=119.136.88.37;f=Edge,s=ctc.ningbo.union.47,c=115.238.190.74
<!html>
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
-I
参数则只是显示http response的头信息
curl -I www.sina.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Fri, 04 Sep 2020 09:18:41 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.sina.com.cn/
Expires: Fri, 04 Sep 2020 09:19:34 GMT
Cache-Control: max-age=120
X-Via-SSL: ssl.22.sinag1.qxg.lb.sinanode.com
Edge-Copy-Time: 1599211120849
Age: 67
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: 159921112132625588877f0beee7358501a1a
X-Cache: HIT.47
X-Via-CDN: f=edge,s=ctc.ningbo.union.47.nb.sinaedge.com,c=119.136.88.37;f=Edge,s=ctc.ningbo.union.47,c=115.238.190.47
4. 显示通信过程
-v
参数可以显示一次http通信的整个过程,包括端口连接和http request头信息。
curl -v www.sina.com
curl --trace output.txt www.sina.com
5. 发送表单信息
- 发送表单信息有GET和POST两种方法。GET方法相对简单,只要把数据附在网址后面就行。
curl example.com/form.cgi?data=xxx
- POST方法必须把数据和网址分开,curl就要用到
--data
参数。
curl -X POST --data "data=xxx" example.com/form.cgi
- 如果你的数据没有经过表单编码,还可以让curl为你编码,参数是
--data-urlencode
。
curl -X POST --data-urlencode "data=xxx" example.com/form.cgi
6. HTTP动词
- curl默认的HTTP动词是GET,使用
-X
参数可以支持其他动词。
curl -X POST www.example.com
curl -X DELETE www.example.com
7. 文件上传
<form method="POST" enctype='multipart/form-data' action="upload.cgi">
<input type=file name=upload>
<input type=submit name=press value="OK">
</form>
curl --form upload=@localfilename --form press=OK [URL]
8. Referer字段
- 有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的。
curl --referer http://www.example.com http://www.example.com
9. User Agent字段
- 这个字段是用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同设备,返回不同格式的网页,比如手机版和桌面版。
curl --user-agent "[User Agent]" [URL]
10. cookie
- 使用
--cookie
参数,可以让curl发送cookie。
curl --cookie "name=xxx" www.example.com
11. 增加头信息
- 有时需要在http request之中,自行增加一个头信息。
--header
参数就可以起到这个作用。
curl --header "Content-Type:application/json" http://example.com
12. HTTP认证
- 有些网域需要HTTP认证,这时curl需要用到
--user
参数。
curl --user name:password example.com
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。