文章自用,看看無妨,建議看看原文。
保存curl命令發(fā)送和接受的所有數(shù)據(jù)到文本save.txt中:
<pre>curl --trace save.txt [url]</pre>
<pre>curl --trace-ascii save.txt [url]</pre>
查看用時(毫秒):
<pre>curl --trace-ascii save.txt --trace-time [url]</pre>
重定向返回數(shù)據(jù)(默認(rèn)輸出到stdout):
<pre>curl [-o | -O] save.txt [url]</pre>
重置主機(jī)IP:
<pre>curl --resolve [host:port]:[new ip] [url]
curl --resolve www.example.org:80:127.0.0.1 http://www.example.org/</pre>
顯示聲明端口號:
<pre>curl [url]:port
curl http://www.example.org:1234/
curl --proxy http://proxy.example.org:4321 http://remote.example.org/</pre>
用戶名和密碼:
<pre>curl http://user:password@example.org/
curl -u user:password http://example.org</pre>
獲取頁面
GET
<pre>curl https://curl.haxx.se 響應(yīng)頭是隱藏的</pre>
<pre>curl --include https://curl.haxx.se 在前面顯示響應(yīng)頭信息</pre>
HEAD
<pre>curl --head(-l) http://curl.haxx.se</pre>
在一條命令行指定多個URL
<pre>curl http://url1.example.com http://url2.example.com
curl --data name=curl http://url1.example.com http://url2.example.com
數(shù)據(jù)會通過POST傳送給每個URL
</pre>
一個命令行使用多個URL,每個URL使用不同的請求方法
<pre>curl -l http://example.com --next http://example.com</pre>
<pre>curl -d score=10 http://example.com/post.cgi --next http://example.com/results.html</pre>
--next類似一個分隔符,分割每個url和數(shù)據(jù),分割的每一塊相當(dāng)于一個新的curl命令。
HTML表單
GET
<form method="GET" action="junk.cgi">
<input type=text name="birthday">
<input type=submit name=press value="ok">
</form>
<pre>curl "http://www.hotmail.com/when/junk.cgi?birthyear=1905&press=ok;"
表單所在頁面的URL是www.hotmail.com/when/birth.html
</pre>
POST
<form method="POST" action="junk.cgi">
<input type=text name="birthyear">
<input type=submit name=press value=" ok ">
</form>
<pre>curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when.cgi</pre>
Content-Type=application/x-www-form-urlencoded]curl默認(rèn)不會將數(shù)據(jù)編碼,如果數(shù)據(jù)中包含空格,必須手動編碼為%20,最新版的curl可以通過下面的參數(shù)將數(shù)據(jù)編碼:
<pre>curl --data-urlencoded "name=I am Daniel" http://www.example.com</pre>
如果多次使用--data參數(shù),curl會使用&將每個--data后面的數(shù)據(jù)合并起來。
POST文件上傳
<from method="POST" enctype='multipart/from-data' action="upload.cgi">
<input type=file name=upload>
<input type=submit name=press value="OK">
</form>
<pre>curl --form upload=@localfilename --form press=OK [url]
注意這個Content-Type是multipart/form-data。
</pre>
隱藏字段
<form method="POST" action="foobar.cgi">
<input type=text name="birthyear">
<input type=hidden name="person" value="daniel">
<input type=submit name="press" value="OK">
</form>
<pre>curl --data "birthyear=1905&press=OK&person=daniel" [url]
對于curl來說隱藏的字段和顯示的字段都是一樣的。</pre>
HTTP認(rèn)證
基礎(chǔ)認(rèn)證
<pre>curl --user name:password http://www.example.com
簡單的混淆,截取后還是完全可讀的。
</pre>
其它認(rèn)證
根據(jù)響應(yīng)頭首部服務(wù)器可能會要求其它的認(rèn)證方法,選擇使用參數(shù)--ntlm、--digest、--negotiate、--anyauth。
代理認(rèn)證
<pre>curl --proxy-user proxyuser:proxypassword curl.haxx.se
如果代理要求NTLM方法認(rèn)證,使用--proxy-ntlm,如果是Digest方法認(rèn)證,使用參數(shù)--proxy-digest。
</pre>
注意
值得注意的是當(dāng)一個程序運(yùn)行的時候,它的參數(shù)可能通過列出系統(tǒng)運(yùn)行的進(jìn)程看到。因此,假如你把用戶認(rèn)證信息作為命令行的參數(shù)傳入,其他用戶是可以看到這些參數(shù)值的。
更多HTTP頭
Referer
<pre>curl --referer [referer url] [url]
curl --referer http://www.example.come http://www.example.com
從上面也可以很容易地看出referer其實(shí)是很容易偽造的。
</pre>
User Agent
讓curl的請求看起來像是從Windows 2000上的IE5發(fā)出的:
<pre>curl --user-agent "Mozilla/4.0(compatible;MSIE 5.01;Windows NT 5.0)" [URL]</pre>
下面使得curl的請求看起來像是從一臺老舊的Linux上Netscape4.73發(fā)出的:
<pre>curl --user-agent "Mozilla/4.73[en](X11;U;Linux2.2.15 i686)" [URL]</pre>
重定向
Location
<pre>curl --location http://www.example.com</pre>
其它重定向
瀏覽器一般還支持兩種重定向的方法而curl不支持:一種是HTML可能會包含meta refresh標(biāo)簽可以讓瀏覽器在指定時間后加載指定URL,另一種是用javascript這樣做。
Cookies
Cookie參數(shù)
一個帶cookie的請求:
<pre>curl --cookie "name=Daniel" http://www.example.com</pre>
通過保存響應(yīng)頭信息來保存cookies,可以使用--dump-header(或者-D)響應(yīng)頭信息:
<pre>curl --dump-header headers_and_cookies.txt http://www.example.com</pre>
使用之前保存的cookies的curl命令:
<pre>curl --cookie stored_cookies.txt http://www.example.com</pre>
貼出原文,沒明白干啥:
Curl's "cookie engine" gets enabled when you use the --cookie option. If you only want curl to understand received cookies, use --cookie with a file that doesn't exist. Example, if you want to let curl understand cookies from a page and follow a location (and thus possibly send back cookies it received), you can invoke it like:
<pre>curl --cookie nada --location http://www.example.com</pre>
curl可以讀寫Netscape和Mozilla曾經(jīng)用過的文件格式的cookie文件。--cookie(-b)可以自動識別給定的文件是否是這種文件格式并解析它,同時使用--cookie-jar(或-c)參數(shù)可以在一個請求操作之后將cookie寫入一個文件。
<pre>curl --cookie cookie.txt --cookie-jar newcookie.txt http://www.example.com</pre>
HTTPS
HTTP是安全的HTTP
HTTP是建立在SSL(或者這個標(biāo)準(zhǔn)的最新版本TLS)上的HTTP,SSL會把通過網(wǎng)絡(luò)發(fā)送和接受的數(shù)據(jù)加密,使得攻擊者很難窺探敏感信息。使用curl從HTTPS服務(wù)器獲得頁面:
<pre>curl https://secure.example.com</pre>
認(rèn)證證書
在HTTPS的世界里,除了一般的密碼外使用證書證明你是誰。curl支持客戶端證書。所有的證書被一個口令保護(hù),這樣在curl使用證書之前你必須通過這個口令。口令可以通過命令行聲明,當(dāng)curl需要使用這個證書的時候會自動進(jìn)入口令驗(yàn)證。在HTTPS服務(wù)器上使用帶證書的curl:
<pre>curl --cert mycert.pem https://secure.example.com</pre>
curl也會通過嚴(yán)重服務(wù)器的證書來證明服務(wù)器聲明的身份,如果驗(yàn)證失敗curl將拒絕和這個服務(wù)器連接,可以使用參數(shù)--insecure(-k)忽略服務(wù)器不能被驗(yàn)證。
更多關(guān)于服務(wù)器證書驗(yàn)證以及ca cert bundles可以參讀SSLCERTS文檔。
還明白什么意思,先粘貼在這里:At times you may end up with your own CA cert store and then you can tell curl to use that to verify the server's certificate:
<pre>curl --cacert ca-bundle.pem https://example.com</pre>
自定義請求元素
改變請求方法和請求頭
通過--header參數(shù)可以自定義請求頭,通過--request可以改變請求方法,下面的例子好好領(lǐng)會一下:
<pre>curl --data "<xml>" --header "Content-Type: text/xml" --request PROPFIND url.com</pre>
上面的例子將POST請求方法變成PROPFIND,發(fā)送的描述實(shí)體主體的首部由默認(rèn)的Content-Type變?yōu)椤盋ontent-Type“。
可以通過提供忽略頭部值的頭部來刪除對應(yīng)首部:
<pre>curl --header "Host:" http://www.example.com</pre>
通過提供新的頭部值來自定義頭部,或者提供帶屬性值的新的頭部來增加頭部:
<pre>curl --header "Destination: http://anywhere.com" http://example.com</pre>
更多的關(guān)于改變請求方法
值得一提的是curl會根據(jù)請求的action來選擇使用哪個請求方法,例如-d時curl會選擇POST方法,-l時curl會選擇HEAD方法,以此類推。假如你使用--request/-X會改變curl選擇的方法關(guān)鍵字,但是并不會改變curl的行為。這也意味著,當(dāng)你用-d"data""去作一個POST請求,你可以通過-X將方法改變?yōu)镻ROPFIND,但是curl還是認(rèn)為它發(fā)出的是一個POST請求。可以改變默認(rèn)GET方法為POST通過如下命令:
<pre>curl -X POST http://example.org</pre>
參考
RFC 7230 如果想要深入理解HTTP協(xié)議這是必讀的文檔
RFC 3986 解釋URL語法
RFC 1867 定義HTTP基于HTML表單的文件上傳
RFC 6525 定義HTTP cookies是如何工作的