1. 获取页面内容
- 当我们不加任何选项使用 curl 时,默认会发送 GET 请求来获取链接内容到标准输出
[root@localhost ~]# curl 192.168.182.10:80
I am villiantsang
2. 显示 HTTP 头
- 如果我们只想要显示 HTTP 头,而不显示文件内容,可以使用
-I
选项:
[root@localhost ~]# curl -I 192.168.182.10:80
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Sat, 22 Feb 2020 05:28:07 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Sat, 22 Feb 2020 05:27:14 GMT
Connection: keep-alive
ETag: "5e50bbb2-12"
Accept-Ranges: bytes
- 同时显示 HTTP 头和文件内容,使用
-i
选项:
[root@localhost ~]# curl -i 192.168.182.10:80
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Sat, 22 Feb 2020 05:28:12 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Sat, 22 Feb 2020 05:27:14 GMT
Connection: keep-alive
ETag: "5e50bbb2-12"
Accept-Ranges: bytes
I am villiantsang
3. 将链接保存到文件
- 我们可以使用
>
符号将输出重定向到本地文件中
[root@localhost ~]# curl -i 192.168.182.10:80 > index.html
通过 curl 自带的-o/-O
选项将内容保存到文件中
-o
(小写的 o):结果会被保存到命令行中提供的文件名
[root@localhost ~]# curl -o index.html 192.168.182.10:80
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 18 100 18 0 0 5972 0 --:--:-- --:--:-- --:--:-- 9000
[root@localhost ~]# ls
index.html
-O
(大写的 O):URL 中的文件名会被用作保存输出的文件名
[root@localhost ~]# curl -O 192.168.182.10/download
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 168 100 168 0 0 118k 0 --:--:-- --:--:-- --:--:-- 164k
[root@localhost ~]# ls
download
注意:使用 -O
选项时,必须确保链接末尾包含文件名,否则 curl 无法正确保存文件。如果遇到链接中无文件名的情况,应该使用 -o
选项手动指定文件名,或使用重定向符号。
4. 同时下载多个文件
[root@localhost ~]# curl -o baidu.html www.baidu.com -O www.jd.com/page
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 4321 0 --:--:-- --:--:-- --:--:-- 4329
100 157 100 157 0 0 548 0 --:--:-- --:--:-- --:--:-- 12076
[root@localhost ~]# ls
baidu.html page
5. 使用 -L 跟随链接重定向
如果直接使用 curl 打开某些被重定向后的链接,这种情况下就无法获取我们想要的网页内容。例如:
[root@localhost ~]# curl http://codebelief.com
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.17.4</center>
</body>
</html>
而当我们通过浏览器打开该链接时,会自动跳转到 http://www.codebelief.com
。此时我们想要 curl
做的,就是像浏览器一样跟随链接的跳转,获取最终的网页内容。我们可以在命令中添加-L
选项来跟随链接重定向:
[root@localhost ~]# curl -L http://codebelief.com
......
......
这样我们就能获取到经过重定向后的网页内容了
6. 自定义 User-Agent
我们可以使用-A
来自定义用户代理,例如下面的命令将伪装成安卓火狐浏览器对网页进行请求:
[root@localhost ~]# curl -A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" http://192.168.182.10:80
查看访问日志
192.168.182.11 - - [22/Feb/2020:13:48:39 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" "-" "-"
7. 自定义 header
当我们需要传递特定的header
的时候,可以仿照以下命令来写:
[root@localhost ~]# curl -H "Referer: www.example.com" -H "User-Agent: Custom-User-Agent" http://192.168.182.11:80
查看访问日志
192.168.182.11 - - [22/Feb/2020:13:51:08 +0800] "GET / HTTP/1.1" 200 612 "www.example.com" "Custom-User-Agent" "-" "-"
可以看到,当我们使用-H
来自定义User-Agent
时,需要使用"User-Agent: xxx"
的格式。
我们能够直接在 header 中传递 Cookie,格式与上面的例子一样:
8.保存 Cookie
当我们使用 cURL 访问页面的时候,默认是不会保存 Cookie 的。有些情况下我们希望保存 Cookie 以便下次访问时使用。例如登陆了某个网站,我们希望再次访问该网站时保持登陆的状态,这时就可以先将登陆时的 Cookie 保存起来,下次访问时再读取。
-c
后面跟上要保存的文件名
[root@localhost ~]# curl -c "cookie-example" http://www.example.com
9、读取 Cookie
前面讲到了使用-H
来发送 Cookie 的方法,这种方式是直接将 Cookie 字符串写在命令中。如果使用-b
来自定义 Cookie,命令如下:
[root@localhost ~]# curl -b "JSESSIONID=D0112A5063D938586B659EF8F939BE24" http://www.example.com
如果要从文件中读取 Cookie,-H
就无能为力了,此时可以使用-b
来达到这一目的:
[root@localhost ~]# curl -b "cookie-example" http://www.example.com
即-b
后面既可以是 Cookie 字符串,也可以是保存了 Cookie 的文件名。
10、发送 POST 请求
我们以登陆网页为例来进行说明使用 cURL 发送 POST 请求的方法。假设有一个登录页面 www.example.com/login,只需要提交用户名和密码便可登录。我们可以使用 cURL 来完成这一 POST 请求,-d 用于指定发送的数据,-X 用于指定发送数据的方式:
curl -d "userName=tom&passwd=123456" -X POST http://www.example.com/login
在使用 -d
的情况下,如果省略-X
,则默认为 POST
方式:
curl -d "userName=tom&passwd=123456" http://www.example.com/login
强制使用GET
方式
curl -d "@data.txt" http://www.example.com/login
带 Cookie
登录
当然,如果我们再次访问该网站,仍然会变成未登录的状态。我们可以用之前提到的方法保存 Cookie
,在每次访问网站时都带上该Cookie
以保持登录状态。
curl -c "cookie-login" -d "userName=tom&passwd=123456" http://www.example.com/login
再次访问该网站时,使用以下命令:
curl -b "cookie-login" http://www.example.com/login
http://www.codebelief.com/article/2017/05/linux-command-line-curl-usage/