Let's Go!

CURL用法

上一篇 / 下一篇  2011-03-15 16:52:06 / 个人分类:系统相关&实用技巧

CURL是一个超强的命令行工具,其功能非常强大,有Linux/Unix版本的,也有Windows版本的,我平时就经常在Windows下面使用curl做一些测试,非常方便,有时用curl做测试比用浏览器做测试要快得多,方便得多。

1.curl命令帮助选项
C:\>curl --help
Usage: curl [options...] <url>
Options: (H) means HTTP/HTTPS only, (F) means FTP only
 -a/--append        Append to target file when uploading (F)
 -A/--user-agent <string> User-Agent to send to server (H)
    --anyauth       Tell curl to choose authentication method (H)
 -b/--cookie <name=string/file> Cookie string or file to read cookies from (H)
    --basic         Enable HTTP Basic Authentication (H)
 -B/--use-ascii     Use ASCII/text transfer
 -c/--cookie-jar <file> Write cookies to this file after operation (H)
 -C/--continue-at <offset> Resumed transfer offset
 -d/--data <data>   HTTP POST data (H)
    --data-ascii <data>   HTTP POST ASCII data (H)
    --data-binary <data>  HTTP POST binary data (H)
    --negotiate     Enable HTTP Negotiate Authentication (H)
    --digest        Enable HTTP Digest Authentication (H)
    --disable-eprt  Prevent curl from using EPRT or LPRT (F)
    --disable-epsv  Prevent curl from using EPSV (F)
 -D/--dump-header <file> Write the headers to this file
    --egd-file <file> EGD socket path for random data (SSL)
    --tcp-nodelay   Set the TCP_NODELAY option
 -e/--referer       Referer URL (H)
 -E/--cert <cert[:passwd]> Client certificate file and password (SSL)
    --cert-type <type> Certificate file type (DER/PEM/ENG) (SSL)
    --key <key>     Private key file name (SSL)
    --key-type <type> Private key file type (DER/PEM/ENG) (SSL)
    --pass  <pass>  Pass phrase for the private key (SSL)
    --engine <eng>  Crypto engine to use (SSL). "--engine list" for list
    --cacert <file> CA certificate to verify peer against (SSL)
    --capath <directory> CA directory (made using c_rehash) to verify
                    peer against (SSL)
    --ciphers <list> SSL ciphers to use (SSL)
    --compressed    Request compressed response (using deflate or gzip)
    --connect-timeout <seconds> Maximum time allowed for connection
    --create-dirs   Create necessary local directory hierarchy
    --crlf          Convert LF to CRLF in upload
 -f/--fail          Fail silently (no output at all) on errors (H)
    --ftp-create-dirs Create the remote dirs if not present (F)
    --ftp-pasv      Use PASV instead of PORT (F)
    --ftp-ssl       Enable SSL/TLS for the ftp transfer (F)
 -F/--form. <name=content> Specify HTTP multipart POST data (H)
    --form-string <name=string> Specify HTTP multipart POST data (H)
 -g/--globoff       Disable URL sequences and ranges using {} and []
 -G/--get           Send the -d data with a HTTP GET (H)
 -h/--help          This help text
 -H/--header <line> Custom header to pass to server (H)
 -i/--include       Include protocol headers in the output (H/F)
 -I/--head          Show document info only
 -j/--junk-session-cookies Ignore session cookies read from file (H)
    --interface <interface> Specify network interface to use
    --krb4 <level>  Enable krb4 with specified security level (F)
 -k/--insecure      Allow curl to connect to SSL sites without certs (H)
 -K/--config        Specify which config file to read
 -l/--list-only     List only names of an FTP directory (F)
    --limit-rate <rate> Limit transfer speed to this rate
 -L/--location      Follow Location: hints (H)
    --location-trusted Follow Location: and send authentication even
                    to other hostnames (H)
 -m/--max-time <seconds> Maximum time allowed for the transfer
    --max-redirs <num> Maximum number of redirects allowed (H)
    --max-filesize <bytes> Maximum file size to download (H/F)
 -M/--manual        Display the full manual
 -n/--netrc         Must read .netrc for user name and password
    --netrc-optional Use either .netrc or URL; overrides -n
    --ntlm          Enable HTTP NTLM authentication (H)
 -N/--no-buffer     Disable buffering of the output stream
 -o/--output <file> Write output to <file> instead of stdout
 -O/--remote-name   Write output to a file named as the remote file
 -p/--proxytunnel   Operate through a HTTP proxy tunnel (using CONNECT)
    --proxy-anyauth  Let curl pick proxy authentication method (H)
    --proxy-basic   Enable Basic authentication on the proxy (H)
    --proxy-digest  Enable Digest authentication on the proxy (H)
    --proxy-ntlm    Enable NTLM authentication on the proxy (H)
 -P/--ftp-port <address> Use PORT with address instead of PASV (F)
 -q                 If used as the first parameter disables .curlrc
 -Q/--quote <cmd>   Send command(s) to server before file transfer (F)
 -r/--range <range> Retrieve a byte range from a HTTP/1.1 or FTP server
    --random-file <file> File for reading random data from (SSL)
 -R/--remote-time   Set the remote file's time on the local output
    --retry <num>   Retry request <num> times if transient problems occur
    --retry-delay <seconds> When retrying, wait this many seconds between each
    --retry-max-time <seconds> Retry only within this period
 -s/--silent        Silent mode. Don't output anything
 -S/--show-error    Show error. With -s, make curl show errors when they occur
    --socks <host[:port]> Use SOCKS5 proxy on given host + port
    --stderr <file> Where to redirect stderr. - means stdout
 -t/--telnet-option <OPT=val> Set telnet option
    --trace <file>  Write a debug trace to the given file
    --trace-ascii <file> Like --trace but without the hex output
 -T/--upload-file <file> Transfer <file> to remote site
    --url <URL>     Spet URL to work with
 -u/--user <user[:password]> Set server user and password
 -U/--proxy-user <user[:password]> Set proxy user and password
 -v/--verbose       Make the operation more talkative
 -V/--version       Show version number and quit
 -w/--write-out [format] What to output after completion
 -x/--proxy <host[:port]> Use HTTP proxy on given port
 -X/--request <command> Specify request command to use
 -y/--speed-time    Time needed to trig speed-limit abort. Defaults to 30
 -Y/--speed-limit   Stop transfer if below speed-limit for 'speed-time' secs
 -z/--time-cond <time> Transfer based on a time condition
 -0/--http1.0       Use HTTP 1.0 (H)
 -1/--tlsv1         Use TLSv1 (SSL)
 -2/--sslv2         Use SSLv2 (SSL)
 -3/--sslv3         Use SSLv3 (SSL)
    --3p-quote      like -Q for the source URL for 3rd party transfer (F)
    --3p-url        source URL to activate 3rd party transfer (F)
    --3p-user       user and password for source 3rd party transfer (F)
 -4/--ipv4          Resolve name to IPv4 address
 -6/--ipv6          Resolve name to IPv6 address
 -#/--progress-bar  Display transfer progress as a progress bar

2.查找页面源代码中的指定内容
例如查找京东商城首页含有js的代码
C:\>curl www.360buy.com | find "js"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
 19  158k   19 31744    0     0  53531      0  0:00:03 --:--:--  0:00:03 65947<s
cript type="text/javascript" src="http://misc.360buyimg.com/201006/js/jquery-1.2
.6.pack.js"></script>
<script. type="text/javascript" src="http://misc.360buyimg.com/201007/js/g.base.j
s"></script>
 76  158k   76  121k    0     0  10763      0  0:00:15  0:00:11  0:00:04  7574<s
pan><a href="http://www.360buy.com/help/ziti/jiangsu.aspx#jssq" target="_blank">
宿迁</a></span>
 99  158k   99  158k   <span><a href="http://www.360buy.com/hel p/ziti/jiangsu.a
spx#jsnt" target="_blank0">南通</a></span>
100  158k  100  158k    0     0  12557      0  0:00:12  0:00:12 --:--:-- 18859
        <script. type="text/javascript" src="http://misc.360buyimg.com/201007/js/
jd.lib.js"></script>
        <script. type="text/javascript" src="http://misc.360buyimg.com/201007/js/
p.index.js"></script>
        <script. type="text/javascript" src="http://wl.360buy.com/wl.js" ></scrip
t>
document.write(unescape("%3Cscript. src='" + gaJsHost + "google-analytics.com/ga.
js' type='text/javascript'%3E%3C/script%3E"));

2.发送POST请求
a.传递一个参加时可以不用双引号
C:\>curl -d action=get_basic_info http://localhost/1616.net/apps/contact/www/mobile.php
[{"contact_id":"3","last_update_time":"1285832338","md5":"7b682e0c3ed3b3bddb3219
a533477224"},{"contact_id":"2","last_update_time":"1286529929","md5":"49ac542f51
19512682b72f1d44e6fe81"},{"contact_id":"1","last_update_time":"1285830870","md5"
:"3926cb3b0320327c46430c6539d58e5e"}]

b.传递多个参加时必须用双引号,否则会报错
C:\>curl -d "action=edit&contact_id=2&name=testurl" http://localhost/1616.net/apps/contact/www/mobile.php
1

3.下载文件
比如下载百度首页的内容为baidu.html
C:\>curl -o baidu.html www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6218  100  6218    0     0  61564      0 --:--:-- --:--:-- --:--:--  173k

4.将网站产生的cookie内容写入到文件中
用curl -c cookie.txt www.baidu.com
产生的文件cookie.txt的内容如下:
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

.baidu.com    TRUE    /    FALSE    2147483647    BAIDUID    3EC2799E83C7187A26CBBA67CCB71822:FG=1

5.测试域名绑定
输入命令
C:\>curl -H "Host:www.baidu.com" http://202.108.22.5/
ip地址202.108.22.5是ping域名www.baidu.com以后返回的ip地址。
则返回百度首页所有的内容

如果随便输入一个域名www.abc.com,而ip地址不改变,则返回内容是空的,因为域名与ip地址不匹配了,这样就可以测试域名绑定是否正确了。
C:\>curl -H "Host:www.abc.com" http://202.108.22.5/
curl: (52) Empty reply from server
查看更多
http://www.blogkid.net/archives/2668.html

6.查看网站头信息
C:\>curl -I www.baidu.com
HTTP/1.1 200 OK
Date: Fri, 08 Oct 2010 15:32:06 GMT
Server: BWS/1.0
Content-Length: 6218
Content-Type: text/html;charset=gb2312
Cache-Control: private
Expires: Fri, 08 Oct 2010 15:32:06 GMT
Set-Cookie: BAIDUID=6E8167DF4E04A22A0659BBA1BE2905E7:FG=1; expires=Fri, 08-Oct-4
0 15:32:06 GMT; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Connection: Keep-Alive

7.查看URL跳转
C:\>curl -L -I www.google.com
HTTP/1.1 302 Found
Location: http://www.google.com.hk/url?sa=p&hl=zh-CN&cki=PREF%3DID%3Dd0fa5e644a9
f891c:FF%3D2:LD%3Dzh-CN:NW%3D1:TM%3D1286551973:LM%3D1286551973:S%3DPQB5WhVsd17Bq
38k&q=http://www.google.com.hk/&ust=1286552003174649&usg=AFQjCNEwGJlI-YF0TG-6BiW
ILw7U2qsr5Q
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=d0fa5e644a9f891c:NW=1:TM=1286551973:LM=1286551973:S=oAe_1PXO
MrFd73Jy; expires=Sun, 07-Oct-2012 15:32:53 GMT; path=/; domain=.google.com
Set-Cookie: NID=39=C_aIB4kMtsJnMR5kwJKF9XAhB9_sEKTp5Qe-Y6Zcu7nNVrrBmKrr-687Zhf_r
-wVNniv4kbb8BRCBR52EN2HdxaL2lGCBxUlEWjkGdZctAqdjyzZbwTb2Hh05UgYMTIO; expires=Sat
, 09-Apr-2011 15:32:53 GMT; path=/; domain=.google.com; HttpOnly
Date: Fri, 08 Oct 2010 15:32:53 GMT
Server: gws
Content-Length: 458
X-XSS-Protection: 1; mode=block

HTTP/1.1 302 Found
Location: http://www.google.com.hk/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=d0fa5e644a9f891c:FF=2:LD=zh-CN:NW=1:TM=1286551973:LM=1286551
973:S=PQB5WhVsd17Bq38k; expires=Sun, 07-Oct-2012 15:32:53 GMT; path=/; domain=.g
oogle.com.hk
Date: Fri, 08 Oct 2010 15:32:53 GMT
Server: gws
Content-Length: 222
X-XSS-Protection: 1; mode=block

HTTP/1.1 200 OK
Date: Fri, 08 Oct 2010 15:32:53 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=Big5
Set-Cookie: PREF=ID=3f9f2340941ea76f:NW=1:TM=1286551973:LM=1286551973:S=m9wrFWJe
Jbk4aFK2; expires=Sun, 07-Oct-2012 15:32:53 GMT; path=/; domain=.google.com.hk
Set-Cookie: NID=39=Mlebw-qjMEK1ABTu-W1YWoQ-Tk27--cOtwLLrDWhmU8y0fqwgeyNz06XBZsYG
9yNwSCO_Ryzzt7q1GUXHgrM2jijr9vmLsW9ZXT2k6pve8f-IrdMyLJok4lRImiskdLR; expires=Sat
, 09-Apr-2011 15:32:53 GMT; path=/; domain=.google.com.hk; HttpOnly
Server: gws
X-XSS-Protection: 1; mode=block
Transfer-Encoding: chunked

curl: (18) transfer closed with outstanding read data remaining

延伸阅读:
http://blog.chinaunix.net/u/5591/showart.php?id=1957520
http://www.blogkid.net/archives/2668.html
http://www.baidu.com/s?wd=curl
http://www.google.com/search?hl=en&source=hp&q=curl
http://blog.s135.com/post/389/

 

 

转载自: http://iamcaihuafeng.blog.sohu.com/160735369.html


TAG:

 

评分:0

我来说两句

Open Toolbar