curl是利用URL语法在命令行方式下工作的开源文件传输工具,支持多种网络协议。它被广泛应用在Unix、多种Linux发行版中,并且有DOS和Win32、Win64下的移植版本。
通过-o/-O选项保存下载的文件到指定的文件中:
-o:将文件保存为命令行中指定的文件名的文件中
-O:使用URL中默认的文件名保存文件到本地
curl -o baidu.txt www.baidu.com
curl -O baidu.txt www.baidu.com/index.html ---> index.html
同时获取多个文件
curl -o baidu.txt www.baidu.com -o 163.txt www.163.com
curl进行重定向
curl命令默认不会重定向 默认情况下CURL不会发送HTTP Location headers(重定向). 可以通过使用-L选项进行强制重定向
1 # 让curl使用地址重定向,此时会查询google.com.hk站点
2 curl -L http://www.google.com
对CURL使用网络限速
通过--limit-rate选项对CURL的最大网络使用进行限制
1 # 下载速度最大不会超过1000B/second
2 curl -O http://starumlreleases-7a0.kxcdn.com/v2.7.0/StarUML-v2.7.0.dmg
CURL授权
在访问需要授权的页面时,可通过-u选项提供用户名和密码进行授权
1 curl -u username:password URL
2
3 # 通常的做法是在命令行只输入用户名,之后会提示输入密码,这样可以保证在查看历史记录时不会将密码泄露
4 curl -u username URL
获取更多信息
通过使用 -v 和 --trace获取更多的链接信息
curl -v -o baidu www.baidu.com
* Rebuilt URL to: www.baidu.com/
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 115.239.211.112...
* Connected to www.baidu.com (115.239.211.112) port 80 (#0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sun, 17 Jul 2016 13:13:09 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: Keep-Alive
< Vary: Accept-Encoding
< Set-Cookie: BAIDUID=D444B606209C6A471C3FA1631F29693C:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BIDUPSID=D444B606209C6A471C3FA1631F29693C; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: PSTM=1468761189; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BDSVRTM=0; path=/
< Set-Cookie: BD_HOME=0; path=/
< Set-Cookie: H_PS_PSSID=1445_20415_17001_15945_11469; path=/; domain=.baidu.com
< P3P: CP=" OTI DSP COR IVA OUR IND COM "
< Cache-Control: private
< Cxy_all: baidu+9e30c10cdcd752d53d68d22886ce1b82
< Expires: Sun, 17 Jul 2016 13:12:12 GMT
< X-Powered-By: HPHP
< Server: BWS/1.1
< X-UA-Compatible: IE=Edge,chrome=1
< BDPAGETYPE: 1
< BDQID: 0xb11d426500028707
< BDUSERID: 0
<
{ [11520 bytes data]
100 99308 0 99308 0 0 410k 0 --:--:-- --:--:-- --:--:-- 412k
* Connection #0 to host www.baidu.com left intact
为CURL设置代理
-x 选项可以为CURL添加代理功能
1 # 指定代理主机和端口
2 curl -x proxyserver:proxyport http://www.baidu.com/
传递请求数据
默认curl使用GET方式请求数据,这种方式下直接通过URL传递数据
可以通过 --data/-d 方式指定使用POST方式传递数据
1 # GET
2 curl -u username https://api.github.com/user?access_token=XXXXXXXXXX
3
4 # POST
5 curl -u username --data "param1=value1¶m2=value" https://api.github.com
6
7 # 也可以指定一个文件,将该文件中的内容当作数据传递给服务器端
8 curl --data @filename https://github.api.com/authorizations
注:默认情况下,通过POST方式传递过去的数据中若有特殊字符,首先需要将特殊字符转义在传递给服务器端,如value值中包含有空格,则需要先将空格转换成%20,如:
1 curl -d "value%201" http://hostname.com
在新版本的CURL中,提供了新的选项 --data-urlencode,通过该选项提供的参数会自动转义特殊字符。
1 curl --data-urlencode "value 1" http://hostname.com
除了使用GET和POST协议外,还可以通过 -X 选项指定其它协议,如:
1 curl -I -X DELETE https://api.github.cim
上传文件
1 curl --form "fileupload=@filename.txt" http://hostname/resource