一、get請求
curl "http://www.baidu.com" 如果這里的URL指向的是一個文件或者一幅圖都可以直接下載到本地
curl -i "http://www.baidu.com" 顯示全部信息
curl -l "http://www.baidu.com" 只顯示頭部信息
curl -v "http://www.baidu.com" 顯示get請求全過程解析
wget "http://www.baidu.com"也可以
二、post請求
curl -d "param1=value1¶m2=value2" "http://www.baidu.com"
三、json格式的post請求
curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' http://domain/apis/users.json
例如:
curl -l -H "Content-type: application/json" -X POST -d '{"ver": "1.0","soa":{"req":"123"},"iface":"me.ele.lpdinfra.prediction.service.PredictionService","method":"restaurant_make_order_time","args":{"arg2":"\"stable\"","arg1":"{\"code\":[\"WIND\"],\"temperature\":11.11}","arg0":"{\"tracking_id\":\"100000000331770936\",\"eleme_order_id\":\"100000000331770936\",\"platform_id\":\"4\",\"restaurant_id\":\"482571\",\"dish_num\":1,\"dish_info\":[{\"entity_id\":142547763,\"quantity\":1,\"category_id\":1,\"dish_name\":\"[0xe7][0x89][0xb9][0xe4][0xbb][0xb7][0xe8][0x85][0x8a][0xe5][0x91][0xb3][0xe5][0x8f][0x89][0xe7][0x83][0xa7][0xe5][0x8f][0x8c][0xe6][0x8b][0xbc][0xe7][0x85][0xb2][0xe4][0xbb][0x94][0xe9][0xa5][0xad]\",\"price\":31.0}],\"merchant_location\":{\"longitude\":\"121.47831425\",\"latitude\":\"31.27576153\"},\"customer_location\":{\"longitude\":\"121.47831425\",\"latitude\":\"31.27576153\"},\"created_at\":1477896550,\"confirmed_at\":1477896550,\"dishes_total_price\":0.0,\"food_boxes_total_price\":2.0,\"delivery_total_price\":2.0,\"pay_amount\":35.0,\"city_id\":\"1\"}"}}' http://vpcb-lpdinfra-stream-1.vm.elenet.me:8989/rpc
ps:json串內層參數需要格式化
用途說明
curl命令是一個功能強大的網絡工具,它能夠通過http、ftp等方式下載文件,也能夠上傳文件。其實curl遠不止前面所說的那些功能,大家可以通過man curl閱讀手冊頁獲取更多的信息。類似的工具還有wget。
curl命令使用了libcurl庫來實現,libcurl庫常用在C程序中用來處理HTTP請求,curlpp是libcurl的一個C++封裝,這幾個東西可以用在抓取網頁、網絡監控等方面的開發,而curl命令可以幫助來解決開發過程中遇到的問題。
常用參數
curl命令參數很多,這里只列出我曾經用過、特別是在shell腳本中用到過的那些。
-A:隨意指定自己這次訪問所宣稱的自己的瀏覽器信息
-b/--cookie <name=string/file> cookie字符串或文件讀取位置,使用option來把上次的cookie信息追加到http request里面去。
-c/--cookie-jar <file> 操作結束后把cookie寫入到這個文件中
-C/--continue-at <offset> 斷點續轉
-d/--data <data> HTTP POST方式傳送數據
-D/--dump-header <file> 把header信息寫入到該文件中
-F/--form <name=content> 模擬http表單提交數據
-v/--verbose 小寫的v參數,用於打印更多信息,包括發送的請求信息,這在調試腳本是特別有用。
-m/--max-time <seconds> 指定處理的最大時長
-H/--header <header> 指定請求頭參數
-s/--slient 減少輸出的信息,比如進度
--connect-timeout <seconds> 指定嘗試連接的最大時長
-x/--proxy <proxyhost[:port]> 指定代理服務器地址和端口,端口默認為1080
-T/--upload-file <file> 指定上傳文件路徑
-o/--output <file> 指定輸出文件名稱
--retry <num> 指定重試次數
-e/--referer <URL> 指定引用地址
-I/--head 僅返回頭部信息,使用HEAD請求
-u/--user <user[:password]>設置服務器的用戶和密碼
-O:按照服務器上的文件名,自動存在本地
-r/--range <range>檢索來自HTTP/1.1或FTP服務器字節范圍
-T/--upload-file <file> 上傳文件
使用示例
1,抓取頁面內容到一個文件中
[root@xi mytest]# curl -o home.html http://www.baidu.com --將百度首頁內容抓下到home.html中
[root@xi mytest]#curl -o #2_#1.jpghttp://cgi2.tky.3web.ne.jp/~{A,B}/[001-201].JPG
由於A/B下的文件名都是001,002...,201,下載下來的文件重名,這樣,自定義出來下載下來的文件名,就變成了這樣:原來: A/001.JPG —-> 下載后: 001-A.JPG 原來: B/001.JPG ---> 下載后: 001-B.JPG
2,用-O(大寫的),后面的url要具體到某個文件,不然抓不下來。還可以用正則來抓取東西
[root@xi mytest]# curl -O http://img.voidcn.com/vcimg/000/000/767/511_420_fe4.gif
運行結果如下:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1575 100 1575 0 0 14940 0 --:--:-- --:--:-- --:--:-- 1538k
會在當前執行目錄中生成一張bdlogo.gif的圖片。
[root@xi mytest]# curl -O http://XXXXX/screen[1-10].JPG --下載screen1.jpg~screen10.jpg
3,模擬表單信息,模擬登錄,保存cookie信息
[root@xi mytest]# curl -c ./cookie_c.txt -F log=aaaa -F pwd=******http://www.XXXX.com/wp-login.php
4,模擬表單信息,模擬登錄,保存頭信息
[root@xi mytest]# curl -D ./cookie_D.txt -F log=aaaa -F pwd=******http://www.XXXX.com/wp-login.php
-c(小寫)產生的cookie和-D里面的cookie是不一樣的。
5,使用cookie文件
[root@xi mytest]# curl -b ./cookie_c.txt http://www.XXXX.com/wp-admin
6,斷點續傳,-C(大寫)
[root@xi mytest]# curl -C -O http://img.voidcn.com/vcimg/000/000/767/511_420_fe4.gif
7,傳送數據,最好用登錄頁面測試,因為你傳值過去后,curl回抓數據,你可以看到你傳值有沒有成功
[root@xi mytest]# curl -d log=aaaa http://www.XXXX.com/wp-login.php
8,顯示抓取錯誤,下面這個例子,很清楚的表明了。
[root@xi mytest]# curl -fhttp://www.XXXX.com/asdf
curl: (22) The requested URL returned error: 404
[root@xi mytest]# curlhttp://www.XXXX.com/asdf
<HTML><HEAD><TITLE>404,not found</TITLE>
9,偽造來源地址,有的網站會判斷,請求來源地址,防止盜鏈。
[root@xi mytest]# curl -ehttp://localhosthttp://www.XXXX.com/wp-login.php
10,當我們經常用curl去搞人家東西的時候,人家會把你的IP給屏蔽掉的,這個時候,我們可以用代理
[root@xi mytest]# curl -x 24.10.28.84:32779 -o home.htmlhttp://www.XXXX.com
11,比較大的東西,我們可以分段下載
[root@xi mytest]# curl -r 0-100 -o img.part1http://www.XXXX.com/wp-content/uploads/2010/09/compare_varnish.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 101 100 101 0 0 105 0 --:--:-- --:--:-- --:--:-- 0
[root@xi mytest]# curl -r 100-200 -o img.part2http://www.XXXX.com/wp-ontent/uploads/2010/09/compare_varnish.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 101 100 101 0 0 57 0 0:00:01 0:00:01 --:--:-- 0
[root@xi mytest]# curl -r 200- -o img.part3http://www.XXXX.com/wp-content/uploads/2010/09/compare_varnish.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 104k 100 104k 0 0 52793 0 0:00:02 0:00:02 --:--:-- 88961
[root@xi mytest]# ls |grep part | xargs du -sh
4.0K one.part1
112K three.part3
4.0K two.part2
用的時候,把他們cat一下就OK,cat img.part* >img.jpg
12,不會顯示下載進度信息
[root@xi mytest]# curl -s -o aaa.jpg http://img.voidcn.com/vcimg/000/000/767/511_420_fe4.gif
13,顯示下載進度條
[root@xi mytest]# curl -0 http://img.voidcn.com/vcimg/000/000/767/511_420_fe4.gif (以http1.0協議請求)
####################################################################### 100.0%
14,通過ftp下載文件
[xifj@Xi ~]$ curl -u用戶名:密碼 -Ohttp://www.XXXX.com/demo/curtain/bbstudy_files/style.css
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
101 1934 101 1934 0 0 3184 0 --:--:-- --:--:-- --:--:-- 7136
[xifj@Xi ~]$ curl -u 用戶名:密碼 -O http://www.XXXX.com/demo/curtain/bbstudy_files/style.css
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
101 1934 101 1934 0 0 3184 0 --:--:-- --:--:-- --:--:-- 7136
或者用下面的方式
[xifj@Xi ~]$ curl -O ftp://用戶名:密碼@ip:port/demo/curtain/bbstudy_files/style.css
[xifj@Xi ~]$ curl -O ftp://用戶名:密碼@ip:port/demo/curtain/bbstudy_files/style.css
15,通過ftp上傳
[xifj@Xi ~]$ curl -T test.sql ftp://用戶名:密碼@ip:port/demo/curtain/bbstudy_files/
[xifj@Xi ~]$ curl -T test.sql ftp://用戶名:密碼@ip:port/demo/curtain/bbstudy_files/
15,模擬瀏覽器頭
[xifj@Xi ~]$ curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -x 123.45.67.89:1080 -o page.html -D cookie0001.txthttp://www.www.baidu.com
16,PUT、GET、POST
比如 curl -T localfile http://cgi2.tky.3web.ne.jp/~zz/abc.cgi,這時候,使用的協議是HTTP的PUT method
剛才說到PUT,自然想起來了其他幾種methos--GET和POST。
http提交一個表單,比較常用的是POST模式和GET模式
GET模式什么option都不用,只需要把變量寫在url里面就可以了
比如:
curl http://www.yahoo.com/login.cgi?user=nick&password=12345
而POST模式的option則是 -d
比如,curl -d "user=nick&password=12345" http://www.yahoo.com/login.cgi
就相當於向這個站點發出一次登陸申請~~~~~
到底該用GET模式還是POST模式,要看對面服務器的程序設定。
一點需要注意的是,POST模式下的文件上的文件上傳,比如
<form method="POST" enctype="multipar/form-data" action="http://cgi2.tky.3web.ne.jp/~zz/up_file.cgi">
<input type=file name=upload>
<input type=submit name=nick value="go">
</form>
這樣一個HTTP表單,我們要用curl進行模擬,就該是這樣的語法:
curl -F upload=@localfile -F nick=go http://cgi2.tky.3web.ne.jp/~zz/up_file.cgi