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/