[備忘]:Linux命令行下常用的http下載工具有wget和curl。
1. 這里用wget遞歸下載某目錄下所有除html外的文件~
wget -r -np --reject=html www.download.example
其中:
-r, 表示遞歸下載當前頁面所有(子)鏈接
-np,表示不去遍歷父目錄下內容
--reject=html,不接受擴展名為html的文件
或者可以把reject換做 --accept=iso,c,h,表示只接受以此結尾的文件,分隔符為逗號(comma-separated)
2. 用curl來查看http過程
curl -v here.is.your.url
我用該選項查看取服務器上主頁的過程,主要是這里包括了http header:
zxluo@polaris:~$ curl -v 192.168.1.1 * About to connect() to 192.168.1.1 port 80 (#0) * Trying 192.168.1.1... connected * Connected to 192.168.1.1 (192.168.1.1) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6 > Host: 192.168.1.1 > Accept: */* > < HTTP/1.1 200 OK < Date: Tue, 26 Feb 2013 07:52:54 GMT < Server: Apache/2.2.16 (Debian) < Last-Modified: Wed, 18 Apr 2012 12:18:11 GMT < ETag: "a002-b1-4bdf30c7c06c0" < Accept-Ranges: bytes < Content-Length: 177 < Vary: Accept-Encoding < Content-Type: text/html < <html><body><h1>It works!</h1> <p>This is the default web page for this server.</p> <p>The web server software is running but no content has been added, yet.</p> </body></html> * Connection #0 to host 192.168.1.1 left intact * Closing connection #0
另外,curl -i here.is.your.url只顯示response的頭信息。
3. 表單提交
發送表單信息有GET和POST兩種方法。GET方法相對簡單,只要把數據附在網址后面就行。
curl example.com/form.cgi?data=xxx
POST方法必須把數據和網址分開,curl就要用到--data參數。
curl --data "data=xxx" example.com/form.cgi
如果你的數據沒有經過表單編碼,還可以讓curl為你編碼,參數是--data-urlencode。
curl --data-urlencode "date=April 1" example.com/form.cgi
你可以用curl這樣上傳文件:
curl --form upload=@localfilename --form press=OK [URL]
4. HTTP認證
有些網域需要HTTP認證,這時curl需要用到--user參數。
curl --user name:password example.com
參考資料:教你學用curl