1.curl是libcurl這個庫支持的镀虐,默認(rèn)支持HTTP1.1(也支持1.0)赁咙。
2.curl支持很多的協(xié)議。curl supports FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP,
LDAPS, FILE, POP3, IMAP, SMTP and RTSP at the time of this writing. Wget supports HTTP, HTTPS and FTP.
curl [option] [url]
1.獲取頁面內(nèi)容
當(dāng)我們不加任何選項使用curl時,默認(rèn)會發(fā)送GET請求來獲取鏈接內(nèi)容到標(biāo)準(zhǔn)輸出
curl localhost:80
2.顯示HTTP頭
option: -I
curl -I localhost:80
結(jié)果
HTTP/1.1 200 OK
Server: nginx/1.17.3
Date: Thu, 12 Sep 2019 10:17:08 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 13 Aug 2019 14:01:07 GMT
Connection: keep-alive
ETag: "5d52c2a3-264"
Accept-Ranges: bytes
3.顯示HTTP頭和文件內(nèi)容
option: -i
curl -i localhost:80
結(jié)果
HTTP/1.1 200 OK
Server: nginx/1.17.3
Date: Thu, 12 Sep 2019 10:28:53 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 13 Aug 2019 14:01:07 GMT
Connection: keep-alive
ETag: "5d52c2a3-264"
Accept-Ranges: bytes
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4.將鏈接保存到文件
通過輸出重定向符號“>”將輸出指定到文件中
curl localhost:80 > index.html
option:-o
curl -o index.html localhost:80
option:-O #URL中要有文件名
curl -O localhost:80/file
5.同時下載多個文件
curl -O localhost:80/file1 -O localhost:80/file2
curl -o file1.html localhost:80/file/1 -o file2.html localhost:80/file/2
6.跟隨鏈接重定向
option -L
curl -L localhost:80
7.自定義header
option -H
curl -H "Host: 118.31.76.144:80" -H "Cookie: token=we8rw9r32ujew8r2jew9823" http://zq.com/html/index.html
8.發(fā)送POST請求
option -d
curl -d "username=root&password=123456" -X POST http://zq.com/login
-d:用于指定發(fā)送的數(shù)據(jù)泪喊,-X:用于指定請求方式
注:在使用-d(默認(rèn)為POST請求)發(fā)送POST請求是可不加-X
如:
curl -d "username=root&password=123456" http://zq.com/login
curl -d "@data.txt" http://zq.com/login #從文件中讀取數(shù)據(jù)
9.發(fā)送GET請求
curl -d "data" -X GET http://zq.com/api #強(qiáng)制使用GET請求
curl -d "data" -G http://zq.com/api
10.讀取 Cookie
option -b #-b后面可以是 Cookie 字符串棚愤,也可以是保存了 Cookie 的文件名
curl -b "token=we8rw9r32ujew8r2jew9823" http://zq.com/html/index.html
curl -b "collie-filename" http://zq.com/html/index.html
11.保存 Cookie
option -c
curl -c "collie-filename" http://zq.com/html/index.html
11.上傳
option --form
curl --form "fileupload=@filename.txt" http://zq.com/resource
常見參數(shù)
-A/--user-agent <string> 設(shè)置用戶代理發(fā)送給服務(wù)器
-b/--cookie <name=string/file> cookie字符串或文件讀取位置
-c/--cookie-jar <file> 操作結(jié)束后把cookie寫入到這個文件中
-C/--continue-at <offset> 斷點續(xù)轉(zhuǎn)
-D/--dump-header <file> 把header信息寫入到該文件中
-e/--referer 來源網(wǎng)址
-f/--fail 連接失敗時不顯示http錯誤
-o/--output 把輸出寫到該文件中
-O/--remote-name 把輸出寫到該文件中,保留遠(yuǎn)程文件的文件名
-r/--range <range> 檢索來自HTTP/1.1或FTP服務(wù)器字節(jié)范圍
-s/--silent 靜音模式戚啥。不輸出任何東西
-T/--upload-file <file> 上傳文件
-u/--user <user[:password]> 設(shè)置服務(wù)器的用戶和密碼
-w/--write-out [format] 什么輸出完成后
-x/--proxy <host[:port]> 在給定的端口上使用HTTP代理
-#/--progress-bar 進(jìn)度條顯示當(dāng)前的傳送狀態(tài)
-z 判斷日期
--limit-rate 下載限速