curl工具的使用
curl是常用的命令行工具,用來請求Web服務器。curl的就是client url的意思。
不帶任何參數時,curl發出的是GET請求
curl https://www.example.com
-A
-A參數指定客戶端的用戶代理標頭,即User-Agent
。curl的默認用戶代理字符串是curl/[version]
# 將User-Agent改成Chrome瀏覽器
curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com
# 移除User-Agent標頭
curl -A '' https://google.com
# 通過-H直接指定標頭
curl -H 'User-Agent:python/3.6'
-b
-b參數用來向服務器發送Cookie
curl -b 'username=qq' https://qq.com
上述命令會生成一個標頭Cookie:username=qq
,向服務器發送一個名為username,值為qq的Cookie
curl -b 'username=qq;qqzone=1' https://qq.com
上面命令發送兩個Cookie
curl -b cookies.txt https://qq.com
上面的命令讀取本地文件cookies.txt,里面是服務器設置的Cookie,將其發送到服務器
-c
-c參數將服務器設置的Cookie寫入一個文件
curl -c cookies.txt https://www.google.com
上述命令將服務器的HTTP響應所設置Cookie寫入本地文本文件cookies.txt
-d
-d參數用於發送POST請求的數據體
curl -d 'login=emmmm&password=23333' -X POST https://google.com/login
# 或者
curl -d 'login=emmmm' -d 'password=23333' -X POST https://google.com/login
使用-d參數以后,HTTP請求會自動加上標頭Content-Type:application/x-www-form-urlencoded
,並且會自動將請求轉化為POST方法,因此可以省略-X POST
。
-d參數也可以讀取本地文本文件的數據,向服務器發送。
curl -d 'data.txt' https://google.com/login
上面命令讀取data.txt文件的內容,作為數據體向服務器發送
--data-urlencode
--data-urlencode
參數等同於-d
,發送POST請求的數據體,區別在於會自動將發送的數據進行URL編碼
curl --data-urlencode 'comment=hello world' https://google.com/login
上面代碼中,發送的數據hello world
之間有一個空格,需要進行 URL 編碼
-e
curl -e 'https://google.com?q=example' https://example.com
上面把Referer
標頭設為https://google.com?q=example
-H
參數可以通過直接添加標頭Referer,達到同樣的效果
curl -H 'Referer:https://google.com?q=example' https://www.example.com
-F
-F參數用來向服務器上傳二進制文件
curl -F 'file=@photo.png' https://google.com/profile
上面命令給HTTP請求加上標頭Content-Type:multipart/form-data
,然后將文件photo.png
作為file字段上傳
-F參數可以指定MIME類型
curl -F 'file=@photo.png;type=image/png' https://google.com/profile
-F參數也可以指定文件名
curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
上面命令中,原始文件名為photo.png,但是服務器接收到的文件名為me.png
-G
-G參數用來構造URL的查詢字符串
curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
上述命令會發出一個GET請求,實際請求的URL為https://google.com/search?q=kitties&count=20
,注意如果忽略-G,會發出一個POST請求
如果數據需要URL編碼。可以結合--data-urlencode
參數
curl -G --data-urlencode 'comment=hello world' https://www.example.com
-H
-H參數添加HTTP請求的標頭
curl -H 'Accept-Language:en-US' https://google.com
上面添加了一個HTTP標頭Accept-Language:en-US
curl -H 'Accept-Language:en-US' -H 'Secret-Message:xyzzy' https://google.com
上面添加兩個HTTP標頭
curl -d '{"login": "emmm", "pass": "2333"}' -H 'Content-Type:application/json'
上面命令添加了一個HTTP
請求標頭Content-Type:application/json
,然后用-d參數發送json
數據
-i
-i參數打印出服務器響應的HTTP標頭
curl -i https://www.example.com
上面命令收到服務器響應后,先輸出服務器響應頭,然后空一行,再網頁的源碼輸出
-I
-I
參數向服務器發出HEAD
請求,然后將服務器返回的HTTP
標頭打印出來
curl -I https://www.example.com
上面命令輸出對HEAD請求的響應
--head
參數等同於-I
curl --head https://www.example.com
-k
參數指定跳過SSL檢測
curl -k https://www.example.com
上面命令不會檢查服務器的SSL證書是否正確
-L
-L
參數會讓HTTP
請求跟隨服務器的重定向。curl默認不跟隨重定向
curl -L -d 'tweet=hi' https://api.twitter.com/tweet
--limit-rate
--limit-rate
用來限制HTTP請求和響應的帶寬,模擬慢網速環境
curl --limit-rate 200k https://google.com
上面命令將帶寬限制在200k
-o
-o參數將服務器的響應保存成文件,等同於wget
命令
curl -o example.html https://www.example.com
上面命令將www.example.com
保存成example.html
-O
-O參數將服務器響應保存成文件,並將URL的最后部分當作文件名
curl -O https://www.example.com/foo/bar.html
上面命令將服務器響應保存成文件,文件名bar.html
-S
-s
參數將不輸出錯誤和進度信息
curl -s https://www.example.com
上面命令一旦發生錯誤,不會顯示錯誤信息。不發生錯誤的話,會正常顯示運行結果
如果想讓curl不產生任何輸出,可以使用下面的命令
curl -s -o /dev/null https://google.com
上面命令沒有任何輸出,除非發生錯誤
-u
-u
參數用來設置服務器認證的用戶名和密碼
curl -u 'bob:123' https://google.com/login
上面命令設置用戶名bob
,密碼為12345
,燃火將其轉為HTTP
標頭,然后將其轉為HTTP標頭Authorization: Basic Ym9iOjEyMzQ1
curl能夠識別URL里面的用戶名和密碼
curl https://bob:123@google.com/login
上面命令能夠識別URL里面的用戶名和密碼,將其轉為上個例子里面的HTTP標頭
curl -u 'bob' https://google.com/login
上面命令只設置了用戶名,執行后,curl 會提示用戶輸入密碼
-v
-v
參數輸出通信的整個過程,用於調試
curl -v https://www.example.com
-x
-x
參數指定HTTP請求的代理
curl -x socks5://james:cat@myproxy.com:8080 https://www.example.com
上面命令指定HTTP請求通過myproxy.com:8080
的socks5代理發出
如果沒有指定代理協議,默認為HTTP
curl -x james:cat@myproxy.com:8080 https://www.example.com
上面命令中,請求的代理使用HTTP協議
-X
-X
參數指定HTTP請求的方法
curl -X POST https://www.example.com
上面命令對https://www.example.com
發出POST請求
補充:
1)監控網頁響應時間
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "https://www.zongze.net"
其中
time_connect 建立到服務器的 TCP 連接所用的時間
time_starttransfer 在發出請求之后,Web 服務器返回數據的第一個字節所用的時間
time_total 完成請求所用的時間
-s 靜默輸出