curl簡介
curl是一個開源的命令行工具,它基於網絡協議,對指定URL進行網絡傳輸,得到數據后不任何具體處理(如:html的渲染等),直接顯示在"標准輸出"(stdout)上。
curl支持的網絡協議有很多,包括:DICT、FILE、FTP、FTPS、GOPHER、GOPHERS、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、MQTT、POP3、POP3S、RTMP、RTMPS、RTSP、SCP、SFTP、SMB、SMBS、SMTP、SMTPS、TELNET和TFTP。
curl的參數也有很多,以下主要介紹HTTP和HTTPS相關的參數,建議收藏保存。
參數速查表
參數 | 描述 | 示例 |
---|---|---|
-A, --user-agent
|
發送指定的User-Agent到服務端 | curl -A "Agent 007" https://one.more |
--alt-svc
|
使用此緩存文件啟用alt-svc | curl --alt-svc svc.txt https://one.more |
--anyauth | 選擇任意認證方法 | curl --anyauth --user me:pwd https://one.more |
--compressed | 請求壓縮 (使用 deflate 或 gzip) | curl --compressed https://one.more |
-b, --cookie <data | filename> | 發送cookie的字符串,或者讀取發送cookie的文件位置 |
-c, --cookie-jar
|
操作結束后寫入cookie到指定的文件 | curl -c cookies.txt https://one.more |
-d, --data | HTTP POST 的數據 | curl -d "name=onemore" https://one.more |
--data-ascii | HTTP POST 的ASCII數據 | curl --data-ascii @file https://one.more |
--data-binary | HTTP POST 的二進制數據 | curl --data-binary @file https://one.more |
--data-raw | HTTP POST 的數據(不對@字符做特殊解析) | curl --data-raw "one@more" https://one.more |
--data-urlencode | HTTP POST 的url編碼的數據 | curl --data-urlencode name=onemore https://one.more |
--digest | 使用HTTP摘要訪問認證 | curl -u name:password --digest https://one.more |
--disallow-username-in-url | 禁止在url中輸入用戶名 | curl --disallow-username-in-url https://one.more |
-D, --dump-header
|
把接收到的header寫入到指定的文件 | curl --dump-header header.txt https://one.more |
-e, --referer
|
指定Referrer的URL | curl --referer "https://more.one" https://one.more |
--etag-compare
|
從文件中讀取作為header中的ETag | curl --etag-compare etag.txt https://one.more |
--etag-save
|
從請求中解析ETag並將其保存到指定的文件中 | curl --etag-save etag.txt https://one.more |
--expect100-timeout
|
允許等待100-continue響應的最大時間(以秒為單位) | curl --expect100-timeout 2.5 https://one.more |
-f, --fail | 在服務器出錯時不顯示HTTP錯誤信息 | curl --fail https://one.more |
-F, --form <name=content> | 模擬用戶按下提交表單 | curl -F image=@onemore.jpg https://one.more |
--form-string <name=string> | 模擬用戶按下提交表單(值為字符串) | curl --form-string "data" https://one.more |
-G, --get | 將POST數據放在URL中並發起GET請求 | curl --get -d "name=onemore" https://one.more |
--haproxy-protocol | 發送HAProxy PROXY v1的header | curl --haproxy-protocol https://one.more |
-i, --include | 在輸出中包含HTTP響應頭 | curl -i https://one.more |
-I, --head | 只顯示響應頭信息 | curl -I https://one.more |
-H, --header header/@file | 發送自定義header | curl -H "Content-Type: application/json;charset=UTF-8" https://one.more |
--hsts
|
啟用HSTS進行傳輸 | curl --hsts cache.txt https://one.more |
--http0.9 | 使用HTTP 0.9 | curl --http0.9 https://one.more |
-0, --http1.0 | 使用HTTP 1.0 | curl --http1.0 https://one.more |
--http1.1 | 使用HTTP 1.1 | curl --http1.1 https://one.more |
--http2 | 使用HTTP 2 | curl --http2 https://one.more |
--http2-prior-knowledge | 使用HTTP 2(不使用HTTP/1.1 Upgrade) | curl --http2-prior-knowledge https://one.more |
--http3 | 使用HTTP 3 | curl --http3 https://one.more |
--ignore-content-length | 忽略服務端資源的大小 | curl --ignore-content-length https://one.more |
-j, --junk-session-cookies | 忽略從文件中讀取的會話cookie | curl --junk-session-cookies -b cookies.txt https://one.more |
-L, --location | 啟用重定向 | curl --L https://one.more |
--location-trusted | 啟用重定向並發送驗證信息到其它主機 | curl --location-trusted -u user:pwd https://one.more |
--max-redirs
|
重定向的最大次數 | curl --max-redirs 3 --location https://one.more |
--negotiate | 使用HTTP Negotiate (SPNEGO) 認證 | curl --negotiate -u : https://one.more |
--no-alpn | 禁用ALPN TLS擴展 | curl --no-alpn https://one.more |
--no-npn | 禁用NPN TLS擴展 | curl --no-npn https://one.more |
--ntlm | 使用HTTP NTLM認證 | curl --ntlm -u user:pwd https://one.more |
--ntlm-wb | 使用HTTP NTLM認證(使用ntlmauth程序做身份驗證) | curl --ntlm-wb -u user:pwd https://one.more |
--post301 | 在301重定向后不切換為 GET 請求 | curl --post301 --location https://one.more |
--post302 | 在302重定向后不切換為 GET 請求 | curl --post302 --location https://one.more |
--post303 | 在303重定向后不切換為 GET 請求 | curl --post303 --location https://one.more |
-r, --range
|
只接收范圍內的字節 | curl --range 22-44 https://one.more |
--raw | 禁用所有內部HTTP對內容或傳輸編碼的解碼 | curl --raw https://one.more |
--tr-encoding | 請求壓縮傳輸編碼算法,並解壓接收到的數據 | curl --tr-encoding https://one.more |
-v, --verbose | 顯示通信的整個過程,包括端口連接和頭信息。 | curl -v https://one.more |
-z, --time-cond | 請求在給定時間之后或之前被修改的文件 | curl -z "Tue 18 Jan 2021 13:14:15" https://one.more |
curl 版本號:7.74.0
竟然已經看到這里了,你我定是有緣人,留下你的點贊和關注,他日必成大器。
微信公眾號:萬貓學社
微信掃描二維碼
關注后回復「電子書」
獲取12本Java必讀技術書籍
