curl命令經常使用操做

curl是作什麼的

curl是利用URL語法在命令行方式下工做的開源文件傳輸工具,它支持http,https,ftp,ftps,telnet等多種協議。
經常使用於模擬提交web數據,與網站API交互(POST/GET請求,能夠自定義發送header),也能夠用於下載(PS:專職下載仍是用wget,支持recursive)。html

經常使用操做

GET請求(-G/--get/省略)

curl http://www.xxxx.com/show?userId=111

POST請求

以application/x-www-url-encoded 方式發送數據(-d/--data):

curl -d "username=sunnyxd&password=12345" URL

以multipart/form-data 的方式發送數據(上傳文件,-F/--form):

curl -F filename=@/home/sunnyxd/file.tar.gz -F username=sunnyxd URL

設置cookie

使用cookie (-b/--cookie)

curl URL -b "username=sunnyxd;password=12345"

保存cookie (-c/--cookie-jar)

curl -d "username=sunnyxd&password=12345" -c ./cookie.txt URL 操做結束後把cookie寫入文件cookie.txt

抓取頁面(下載)

抓取頁面保存到test.html:linux

curl -o test.html URL
或者curl URL > test.html

-O 下載特定文件,url須要指定到一個具體的文件
-C - 斷點續傳,- 自動推斷出正確的續傳位置,或者直接指定相應的字節偏移
-f 顯示抓取錯誤信息
-x ip:port 使用代理
-s 不顯示進度信息
-e/--referer 僞造來源地址
--limit-rate 50k 限制下載速度
--max-filesize bytes 指定可下載的最大文件大小

格式化顯示響應信息

-w 一次完整且成功的操做後輸出指定格式的內容到標準輸出。web

查看接口響應時間

curl -o /dev/null -s -w "%{time_connect}:%{time_starttransfer}:%{time_total}\n" URL
第一個字段,是從命令啓動到連接上用的時間
第二個字段,是開始傳輸數據所用的時間
第三個字段,是完成傳輸所用的時間

查看頁面是否可用

curl -o /dev/null -s -w %{http_code} URL

監控接口可用性的一個簡單demo:瀏覽器

#!/bin/bash
echo "check result:"
cat monitor_url | while read line
do
status_code=`curl -o /dev/null -s -w %{http_code} $line`
if [ $status_code -eq 200 ]
then
echo ${line}"is ok"
else
echo ${line}"is fail"
fi
done

curl -w詳細介紹:http://www.letuknowit.com/post/17.htmlbash

設置瀏覽器代理 (-A/--user-agent)

curl URL -A "Mozilla/5.0

只打印響應頭部信息

經過-I或者--head能夠只打印出HTTP頭部信息:cookie

curl -I URL

用戶認證(-u/--user)

用於HTTP或者FTP的認證,能夠指定密碼,也能夠不指定密碼在後續操做中輸入密碼:app

curl -u user:pwd URL
curl -u user URL

通用頭部信息傳遞(-H/--header)

curl -H "Host:127.0.0.1" -H "accept-language:zh-cn" URL

自動跳轉到新網址

有的網址是自動跳轉的。使用-L參數,curl就會跳轉到新的網址。curl

curl -L URL

設置請求超時時間

curl --connect-timeout seconds URL

設置最大傳輸時間(-m/--max-time)

curl -m seconds URL

參數詳細介紹請看這裏:http://man.linuxde.net/curl
參考文章:http://blog.csdn.net/xifeijian/article/details/9367339工具

相關文章
相關標籤/搜索