curl 介紹和基本用法

curl是一個廣泛使用的用來上傳和下載的命令行工具,當然嚴格來講,它還可以有別的用途。對於測試來講,它是Web相關測試非常實用的工具,包括debugging,使用起來非常方便。而且另一方面,因爲它是純命令行的工具,所以也可以非常方便的作爲一個組件集成到automation或者其他的測試框架裏面,將HTTP/HTTPS/FTP相關的上傳和下載等任務交給它。
最近因爲在做web service相關的測試,用到了curl,覺得還不錯,順便給大家介紹一下。
 
official site: http://curl.haxx.se/ , 目前最新的版本是7.20.0。


基本的用法示例

curl http://www.google.com

curl dict://dict.org/d:test

下面主要介紹一下常用的參數,關於這方面,可以查看它自帶的幫助文檔。
-V show the version of curl
C:/Tools/curl-7.20.0>curl -V
curl 7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3
Protocols: dict file ftp ftps http https imap imaps ldap pop3 pop3s rtsp smtp smtps telnet tftp
Features: AsynchDNS Largefile NTLM SSL SSPI libz



-o [filename], save content to a file
-O use the same name

curl -o g.html www.google.com

curl -O http://www.google.com/intl/en_ALL/images/logo.gif


-i, Include the HTTP-header in the output     
curl -i http://www.google.com

-u username:password
curl -u user:pwd ftp://myftp/Ricky/test.ini

 

-d parameters
curl http://www.yahoo.com/login.cgi?user=nick&password=12345
curl -d "user=nick&password=12345" http://www.yahoo.com/login.cgi


-x  proxy_ip:port, access with proxy

curl -i -x myproxy:8080 -o google_proxy.html http://www.google.com

 

HTTP/1.1 302 Found
Via: 1.0 myproxy
Content-Length: 222
Date: Mon, 22 Mar 2010 14:15:48 GMT
Location: http://www.google.com.tw/
Content-Type: text/html; charset=UTF-8
Cache-Control: private
Set-Cookie: PREF=ID=aa97c3734c862ddf:TM=1269267348:LM=1269267348:S=4FEEGAPNu2FN2lho; expires=Wed, 21-Mar-2012 14:15:48 GMT; path=/; domain=.google.com
Set-Cookie: NID=32=MXKkZjgjiNGVNnPv9w384COa2KJZOLu5v9_5coX_N1tJtaa97-dJUxb7DCz90vQsm0fLEPg0Ee3nXv1yDDcE3ZN0sOu7mq9-mjB9CL0okXGjiIvox2FTcw0HBV7hBaLC; expires=Tue, 21-Sep-2010 14:15:48 GMT; path=/; domain=.google.com; HttpOnly
Server: gws
X-XSS-Protection: 0

 

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.tw/">here</A>.
</BODY></HTML>

 

-r [start-end], such as "-r 0-1024"

curl -i -r 0-1024 -o rfc1.txt http://xml.resource.org/public/rfc/bibxml3/rdf/item.I-D.6man-pmip6-ind.rdf

header:
HTTP/1.1 206 Partial Content
Date: Mon, 22 Mar 2010 13:50:46 GMT
Server: Apache/2.2.15 (Debian)
Last-Modified: Mon, 09 Mar 2009 23:02:11 GMT
ETag: "148be6-466-464b79fe1a2c0"
Accept-Ranges: bytes
Content-Length: 1025
Content-Range: bytes 0-1024/1126
Content-Type: application/rdf+xml

note: not all web server support partial content, or it will response all even you try to get part
curl -i -r 0-1024 http://www.sina.com.cn -o sina_part1.htm

it will give you all the page.

-k insecure SSL , will not check the cert

curl -k https://ip:8445

 

-T upload a file with FTP
curl -T curl.html -u user:pwd ftp://myftp/Ricky/
execute once again will overwrite


-F/--form <name=content>, use for http post

curl -F password=@/etc/passwd www.mypasswords.com

You can also tell curl what Content-Type to use by using 'type=', in a manner similar to: 

curl -F "[email protected];type=text/html" url.com  

 
最後順便說一下,如果是用來做HTTP的測試和調試,希望有GUI的話,也有不少其它的工具可以選擇,包括Fiddler,JMeter和Firefox的tamper data插件。

http://blog.csdn.net/superqa/article/details/5418636
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章