Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux curl 指令完整實戰匯總:下載、上傳、API 測試、Cookie/Proxy/偽裝瀏覽器

編輯:關於 Linux

curl 是支援 HTTP/HTTPS/FTP/SFTP 等協定的命令列傳輸工具,參數豐富,適用於自動化腳本、API 除錯、檔案下載上傳、爬蟲開發。

核心參數速查

參數功能
-o file輸出到指定檔名
-O使用遠端檔名儲存
-C -斷點續傳
-H "Header: val"自訂請求標頭
-A "UA string"偽裝 User-Agent
-e "referer"偽裝 Referer (防盜鏈)
-b file / -c file讀取/儲存 Cookie
-x host:port指定 HTTP Proxy
-u user:passBasic Auth 認證
-T file上傳檔案 (PUT)
-X METHOD指定 HTTP 方法 (POST/PUT/DELETE)
-d "data"POST 資料
-s靜默模式 (不顯示進度)
-w "%{http_code}"自訂輸出格式 (如狀態碼)
-#進度條顯示

實戰範例

# 1. 基本下載並顯示
curl https://example.com

# 2. 儲存網頁
curl -o page.html https://example.com
curl -O https://example.com/file.zip

# 3. 測試 HTTP 狀態碼 (腳本適用)
curl -o /dev/null -s -w "%{http_code}" https://example.com

# 4. 使用 Proxy
curl -x 192.168.1.100:8080 https://example.com

# 5. Cookie 操作
curl -c cookies.txt https://example.com/login -d "user=foo&pass=bar"
curl -b cookies.txt https://example.com/profile

# 6. 偽裝瀏覽器
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" https://example.com

# 7. 偽裝 Referer (防盜鏈)
curl -e "https://example.com/" https://example.com/image.jpg

# 8. 批次下載 (花括號展開)
curl -O "https://example.com/img[1-5].jpg"
curl -o "#1_#2.jpg" "https://example.com/{a,b}/img[1-5].jpg"  # 重命名避免覆蓋

# 9. POST JSON API
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/endpoint

# 10. 上傳檔案
curl -T localfile.txt ftp://user:pass@ftp.example.com/remotefile.txt

curl 參數

進階技巧: --limit-rate 100k 限速;--retry 3 --retry-delay 5 失敗重試;--cert --key 客戶端憑證認證;--config 讀取參數檔簡化長指令;結合 jq 處理 JSON 回應:curl -s api | jq .data。
Copyright © Windows教程網 All Rights Reserved