Linux命令行:cURL的常見用法

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
  • --trace 命令能夠查看更詳細的通訊過程
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這樣上傳文件:
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
相關文章
相關標籤/搜索