【无标题】

curl命令的一些使用

# curl help
Usage: curl [options...] <url>
     --abstract-unix-socket <path> Connect via abstract Unix domain socket
     --anyauth       Pick any authentication method
 -a, --append        Append to target file when uploading
     --basic         Use HTTP Basic Authentication
     --cacert <CA certificate> CA certificate to verify peer against
     --capath <dir>  CA directory to verify peer against
 -E, --cert <certificate[:password]> Client certificate file and password
     --cert-status   Verify the status of the server certificate
     --cert-type <type> Certificate file type (DER/PEM/ENG)
     --ciphers <list of ciphers> SSL ciphers to use
     --compressed    Request compressed response
 -K, --config <file> Read config from a file
     --connect-timeout <seconds> Maximum time allowed for connection
     --connect-to <HOST1:PORT1:HOST2:PORT2> Connect to host
 -C, --continue-at <offset> Resumed transfer offset
 -b, --cookie <data> Send cookies from string/file
 -c, --cookie-jar <filename> Write cookies to <filename> after operation
     --create-dirs   Create necessary local directory hierarchy
     --crlf          Convert LF to CRLF in upload
     --crlfile <file> Get a CRL list in PEM format from the given file
 -d, --data <data>   HTTP POST data
     --data-ascii <data> HTTP POST ASCII data
     --data-binary <data> HTTP POST binary data
     --data-raw <data> HTTP POST data, '@' allowed
     --data-urlencode <data> HTTP POST data url encoded
     --delegation <LEVEL> GSS-API delegation permission
     --digest        Use HTTP Digest Authentication
 -q, --disable       Disable .curlrc
     --disable-eprt  Inhibit using EPRT or LPRT
     --disable-epsv  Inhibit using EPSV
     --dns-interface <interface> Interface to use for DNS requests
     --dns-ipv4-addr <address> IPv4 address to use for DNS requests
     --dns-ipv6-addr <address> IPv6 address to use for DNS requests
     --dns-servers <addresses> DNS server addrs to use
 -D, --dump-header <filename> Write the received headers to <filename>
     --egd-file <file> EGD socket path for random data
     --engine <name> Crypto engine to use
     --expect100-timeout <seconds> How long to wait for 100-continue
 -f, --fail          Fail silently (no output at all) on HTTP errors
     --fail-early    Fail on first transfer error, do not continue
     --false-start   Enable TLS False Start
 -F, --form <name=content> Specify HTTP multipart POST data
     --form-string <name=string> Specify HTTP multipart POST data
     --ftp-account <data> Account data string
     --ftp-alternative-to-user <command> String to replace USER [name]
     --ftp-create-dirs Create the remote dirs if not present
     --ftp-method <method> Control CWD usage
     --ftp-pasv      Use PASV/EPSV instead of PORT
 -P, --ftp-port <address> Use PORT instead of PASV
     --ftp-pret      Send PRET before PASV
     --ftp-skip-pasv-ip Skip the IP address for PASV
     --ftp-ssl-ccc   Send CCC after authenticating
     --ftp-ssl-ccc-mode <active/passive> Set CCC mode
     --ftp-ssl-control Require SSL/TLS for FTP login, clear for transfer
 -G, --get           Put the post data in the URL and use GET
 -g, --globoff       Disable URL sequences and ranges using {} and []
 -I, --head          Show document info only
 -H, --header <header/@file> Pass custom header(s) to server
 -h, --help          This help text
     --hostpubmd5 <md5> Acceptable MD5 hash of the host public key
 -0, --http1.0       Use HTTP 1.0
     --http1.1       Use HTTP 1.1
     --http2         Use HTTP 2
     --http2-prior-knowledge Use HTTP 2 without HTTP/1.1 Upgrade
     --ignore-content-length Ignore the size of the remote resource
 -i, --include       Include protocol response headers in the output
 -k, --insecure      Allow insecure server connections when using SSL
     --interface <name> Use network INTERFACE (or address)
 -4, --ipv4          Resolve names to IPv4 addresses
 -6, --ipv6          Resolve names to IPv6 addresses
 -j, --junk-session-cookies Ignore session cookies read from file
     --keepalive-time <seconds> Interval time for keepalive probes
     --key <key>     Private key file name
     --key-type <type> Private key file type (DER/PEM/ENG)
     --krb <level>   Enable Kerberos with security <level>
     --libcurl <file> Dump libcurl equivalent code of this command line
     --limit-rate <speed> Limit transfer speed to RATE
 -l, --list-only     List only mode
     --local-port <num/range> Force use of RANGE for local port numbers
 -L, --location      Follow redirects
     --location-trusted Like --location, and send auth to other hosts
     --login-options <options> Server login options
     --mail-auth <address> Originator address of the original email
     --mail-from <address> Mail from this address
     --mail-rcpt <address> Mail from this address
 -M, --manual        Display the full manual
     --max-filesize <bytes> Maximum file size to download
     --max-redirs <num> Maximum number of redirects allowed
 -m, --max-time <time> Maximum time allowed for the transfer
     --metalink      Process given URLs as metalink XML file
     --negotiate     Use HTTP Negotiate (SPNEGO) authentication
 -n, --netrc         Must read .netrc for user name and password
     --netrc-file <filename> Specify FILE for netrc
     --netrc-optional Use either .netrc or URL
 -:, --next          Make next URL use its separate set of options
     --no-alpn       Disable the ALPN TLS extension
 -N, --no-buffer     Disable buffering of the output stream
     --no-keepalive  Disable TCP keepalive on the connection
     --no-npn        Disable the NPN TLS extension
     --no-sessionid  Disable SSL session-ID reusing
     --noproxy <no-proxy-list> List of hosts which do not use proxy
     --ntlm          Use HTTP NTLM authentication
     --ntlm-wb       Use HTTP NTLM authentication with winbind
     --oauth2-bearer <token> OAuth 2 Bearer Token
 -o, --output <file> Write to file instead of stdout
     --pass <phrase> Pass phrase for the private key
     --path-as-is    Do not squash .. sequences in URL path
     --pinnedpubkey <hashes> FILE/HASHES Public key to verify peer against
     --post301       Do not switch to GET after following a 301
     --post302       Do not switch to GET after following a 302
     --post303       Do not switch to GET after following a 303
     --preproxy [protocol://]host[:port] Use this proxy first
 -#, --progress-bar  Display transfer progress as a bar
     --proto <protocols> Enable/disable PROTOCOLS
     --proto-default <protocol> Use PROTOCOL for any URL missing a scheme
     --proto-redir <protocols> Enable/disable PROTOCOLS on redirect
 -x, --proxy [protocol://]host[:port] Use this proxy
     --proxy-anyauth Pick any proxy authentication method
     --proxy-basic   Use Basic authentication on the proxy
     --proxy-cacert <file> CA certificate to verify peer against for proxy
     --proxy-capath <dir> CA directory to verify peer against for proxy
     --proxy-cert <cert[:passwd]> Set client certificate for proxy
     --proxy-cert-type <type> Client certificate type for HTTS proxy
     --proxy-ciphers <list> SSL ciphers to use for proxy
     --proxy-crlfile <file> Set a CRL list for proxy
     --proxy-digest  Use Digest authentication on the proxy
     --proxy-header <header/@file> Pass custom header(s) to proxy
     --proxy-insecure Do HTTPS proxy connections without verifying the proxy
     --proxy-key <key> Private key for HTTPS proxy
     --proxy-key-type <type> Private key file type for proxy
     --proxy-negotiate Use HTTP Negotiate (SPNEGO) authentication on the proxy
     --proxy-ntlm    Use NTLM authentication on the proxy
     --proxy-pass <phrase> Pass phrase for the private key for HTTPS proxy
     --proxy-service-name <name> SPNEGO proxy service name
     --proxy-ssl-allow-beast Allow security flaw for interop for HTTPS proxy
     --proxy-tlsauthtype <type> TLS authentication type for HTTPS proxy
     --proxy-tlspassword <string> TLS password for HTTPS proxy
     --proxy-tlsuser <name> TLS username for HTTPS proxy
     --proxy-tlsv1   Use TLSv1 for HTTPS proxy
 -U, --proxy-user <user:password> Proxy user and password
     --proxy1.0 <host[:port]> Use HTTP/1.0 proxy on given port
 -p, --proxytunnel   Operate through a HTTP proxy tunnel (using CONNECT)
     --pubkey <key>  SSH Public key file name
 -Q, --quote         Send command(s) to server before transfer
     --random-file <file> File for reading random data from
 -r, --range <range> Retrieve only the bytes within RANGE
     --raw           Do HTTP "raw"; no transfer decoding
 -e, --referer <URL> Referrer URL
 -J, --remote-header-name Use the header-provided filename
 -O, --remote-name   Write output to a file named as the remote file
     --remote-name-all Use the remote file name for all URLs
 -R, --remote-time   Set the remote file's time on the local output
 -X, --request <command> Specify request command to use
     --request-target Specify the target for this request
     --resolve <host:port:address> Resolve the host+port to this address
     --retry <num>   Retry request if transient problems occur
     --retry-connrefused Retry on connection refused (use with --retry)
     --retry-delay <seconds> Wait time between retries
     --retry-max-time <seconds> Retry only within this period
     --sasl-ir       Enable initial response in SASL authentication
     --service-name <name> SPNEGO service name
 -S, --show-error    Show error even when -s is used
 -s, --silent        Silent mode
     --socks4 <host[:port]> SOCKS4 proxy on given host + port
     --socks4a <host[:port]> SOCKS4a proxy on given host + port
     --socks5 <host[:port]> SOCKS5 proxy on given host + port
     --socks5-basic  Enable username/password auth for SOCKS5 proxies
     --socks5-gssapi Enable GSS-API auth for SOCKS5 proxies
     --socks5-gssapi-nec Compatibility with NEC SOCKS5 server
     --socks5-gssapi-service <name> SOCKS5 proxy service name for GSS-API
     --socks5-hostname <host[:port]> SOCKS5 proxy, pass host name to proxy
 -Y, --speed-limit <speed> Stop transfers slower than this
 -y, --speed-time <seconds> Trigger 'speed-limit' abort after this time
     --ssl           Try SSL/TLS
     --ssl-allow-beast Allow security flaw to improve interop
     --ssl-no-revoke Disable cert revocation checks (WinSSL)
     --ssl-reqd      Require SSL/TLS
 -2, --sslv2         Use SSLv2
 -3, --sslv3         Use SSLv3
     --stderr        Where to redirect stderr
     --suppress-connect-headers Suppress proxy CONNECT response headers
     --tcp-fastopen  Use TCP Fast Open
     --tcp-nodelay   Use the TCP_NODELAY option
 -t, --telnet-option <opt=val> Set telnet option
     --tftp-blksize <value> Set TFTP BLKSIZE option
     --tftp-no-options Do not send any TFTP options
 -z, --time-cond <time> Transfer based on a time condition
     --tls-max <VERSION> Use TLSv1.0 or greater
     --tlsauthtype <type> TLS authentication type
     --tlspassword   TLS password
     --tlsuser <name> TLS user name
 -1, --tlsv1         Use TLSv1.0 or greater
     --tlsv1.0       Use TLSv1.0
     --tlsv1.1       Use TLSv1.1
     --tlsv1.2       Use TLSv1.2
     --tlsv1.3       Use TLSv1.3
     --tr-encoding   Request compressed transfer encoding
     --trace <file>  Write a debug trace to FILE
     --trace-ascii <file> Like --trace, but without hex output
     --trace-time    Add time stamps to trace/verbose output
     --unix-socket <path> Connect through this Unix domain socket
 -T, --upload-file <file> Transfer local FILE to destination
     --url <url>     URL to work with
 -B, --use-ascii     Use ASCII/text transfer
 -u, --user <user:password> Server user and password
 -A, --user-agent <name> Send User-Agent <name> to server
 -v, --verbose       Make the operation more talkative
 -V, --version       Show version number and quit
 -w, --write-out <format> Use output FORMAT after completion
     --xattr         Store metadata in extended file attributes
基本使用
# 1.查看网页源码
    curl www.baidu.com
    # 将网页保存下来,使用 -o 参数,相当于wget
    surl -o [] www.baidu.com
    
# 2.自动跳转 
    #使用'-L',curl就会跳转到新的网址
    curl -L www.sina.com
    结果跳至 www.cina.com.cn
    
# 3.显示头信息 
    #'-i' 参数可以显示http response的头信息,连同网页代码一起
    curl -i www.baidu.com
    # '-I' 参数则是只显示http response的头信息
    
# 4.显示通信过程 
    # '-v'参数可以显示一次http通信的整个过程,包括端口连接和http request头信息
    curl -v www.baidu.com
    #    更详细的通信过程
    curl --trace output.txt www.baidu.com
    curl --trace-ascii output.txt www.baidu.com
    
# 5.发送表单信息
    #  get方法,只要把数据附在网址后面
    curl example.com/from.cgi?data=
    #  post方法,curl就要用到--data参数
    curl -X POST --data "data=xxx" example.com/form.cgi
    #  如果你的数据没有经过表单编码,还可以让curl位你编码,参数是'--data-urlencude'
    curl -X POST--data--urlencode "data=April 1" example.com/form.cgi
    
# 6.http动词  
    #curl默认的http动词时get,使用-X参数可以支持其他动词
    curl -X POST www.example.com
    curl -X DELETE www.example.com
    
# 7.文件上传
    # 假定文件上传的表单是下面这样:
    <form method="POST" enctype='multipart/form-data' action="upload.cgi">
        <input type=file name=upload>
        <input type=submit name=press value="OK">
    </form>
    curl --from upload=@localfilename --from press=OK [url]
    
# 8.referer字段 
    #有时你需要在http request头信息中,提供一个referer字段,便是你是从哪里跳转过来的
    curl --referer http://www.example.com http://www.example.com
# 9.User Agent 
    #表示客户端的设备信息。服务器有时会根据这个字段,针对不同的设备,返回不同格式的网页,比如手机版和桌面版
    curl --user-agent "[User Agent]" [url]

# 10.cookie
    # 使用'--cookie'参数,可以让curl发送cookie
    curl --cookie "name=XXX" www.example.com
    # cookie的值可以从http response头信息的'Set-Cookie'字段中得到。 ’-c cookie-file'可以保存服务器返回的cookie到文件, '-b cookie-file'可以使用这个文件作为cookie信息,进行后续的请求
    curl -c cookies http://example.com
    curl -b cookies http://example.com
    
# 11.增加头信息 
    # 有时需要在http request之中,自行增加一个头信息。'--header'参数就可以起到这个作用
    curl --header "Content-Type:application/json" http://example/com
    
# 12.http认证 有些网域需要http认证,这是curl需要用到'--user'参数
    curl --user name:password example.com
    
    
信息来源:http://www.ruanyifeng.com/blog/2011/09/curl.html

用法
# 不带有任何参数,curl就是发出GET请求
curl https://www.example/com

-A 
   -A参数指定客户端的用户代理标头,即User-Agent. curl的默认用户代理字符串是curl/[version]
   
   curl -A 'Mozilla/5.0...' https://google.com
   上面的命令将User-Agent改成chrome浏览器
   
   curl -A '' https://google.com
   上面的命令会移除User-Agent标头
   
   也可以通过—H参数直接指定标头,更改User—Agent
   surl -H 'User-Agent: php/1.0' https://google.com

-b
   -b参数用来向服务器发送Cookie
   curl -b 'foo=bar' https://google/com
   上面命令会生成一个标头 Cookie: foo=bar, 向服务器发送一个名为foo、值为bar的Cookie
   
   curl -b 'foo=bar,foo2=bar2' https://google.com
   上面命令发送两个Cookie
   
   curl -b cookies.txt https://www.google.com
   上面命令读取本地文件 cookies.txt ,里面是服务器设置的 Cookie(参见 -c 参数),将其发送到服务器
   
-c
   -c 参数将服务器设置的Cookie写入一个文件
   curl -c cookie.txt https://www.goole.com
   上面命令将服务器的Http回应所设置Cookie写入文本文件 cookie.txt
   
-d 
   -d 参数用于发送POST请求的数据体
   
   curl -d 'login=emm&password=123' -X POST https://google.com/login
   curl -d 'login=emm' -d 'password=123' -X POST https://google.com/login
   
   使用 -d 参数以后,HTTP请求会自动加上标头 Content-Type : application/x-www-form-urlencoded .并且会自动将请求转为POST方法,因此可以省略 -X POST
   -d 参数可以读取本地文本文件的数据,向服务器发送
   curl -d '@data.txt' https://google.com/login
   上面命令读取 data.txt 文件的内容,作为数据向服务器发送
   
--data-urlencode
   --data-urlencode参数等同于-d,发送POST请求的数据体,区别在于会自动将发送的数据进行URL编码。
   curl --data-urlencode 'comment=hello world' https://google.com/login
   上面代码中,发送的数据 hello world 之间有一个空格,需要URL编码
   
-e
  -e 参数用来设置HTTP的标头Referer,表示请求的来源
  curl -e 'https://google?q=example' https://www.goolge.com
  上面的命令将Referer标头设为https://google.com?q=example
  -H参数可以通过直接添加标头Referer,达到同样效果
  curl -H 'Referer:https://google.com?q=example' https://www.example.com
  
-F
   -F 参数用来向服务器上传二进制文件
   curl -F 'file=@photo.png' https://google.com/profile
   # 上面命令会给http请求加上标头 Content-Type:  multipart/from-data ,然后将文件 photo.png 作为file字段上传
   -F 参数可以指MIME类型
   curl -F 'file=@photo.png;type=image/png' https://google.com/profile
   -F 参数也可以指定文件名
   curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
   上面命令中,原始文件名位photo.png,但是服务器接收到的文件名位me.png
   
-G 
   -G 参数用来构造url的查询字符串.
   curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
   上面命令会发出一个GET请求,实际请求的url为https://google.com/search?q=kittie&count=20.如果省略 -G ,会发出一个POST请求
   如果数据需要url编码,可以结合 --data--urlencode 参数
   curl -G --data-urlencode 'comment=hello world' https://www.example.com
   
-H
   -H 参数添加HTTP请求的标头
   surl -H 'Accept-Language: en-US' https://googel.com
   上面命令添加HTTP标头 Accept-Language: en-US
   curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzt' https://googel.com
   上面命令添加两个http标头
   curl -d '{"login": "emma", "pass":"123"}' -H 'Content-Type: application/json' https://googel.com
   上面命令添加http请求的标头是Content-Type: applcation/josn ,然后用 -d 参数发送JSON数据
   
-i
   -i参数打印服务器回应的http标头
   curl -i https://www.example.com
   上面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页源码.

-I 
   -I参数向服务器发出head请求,然后服务器返回的HTTP标头打印出来.
   curl -I https://www.example.com
   上面命令输出服务器对HEAD请求的回应
   --head参数等同于 -I
   curl --head https://www.example.com
   
-k 
   -k 参数指定跳过 SSL 检测
   curl -k https://www.exampele.com
   上面命令不会检查服务器的ssl证书是否正确
   
-L
   -L参数会让HTTP请求跟随服务器的重定向. curl默认不跟随重定向.
   curl -L -d 'tweet=hi' https://api.twitter.com/tweet
   
--limit-rate
   --limit-rate 用来限制HTTP请求和回应的带宽,模拟慢网速的环境
   curl --limit-rate 200l https://google.com
   上面命令将带宽限制在每秒200K字节
   
-o
  -o 参数将服务器的回应保存成文件,等同于 wget 命令
  curl -o example.html https://www.example.com
  上面命令将www.example.com保存成example.html
  
-O 
  -O参数将服务器回应保存成文件,并将URL的最后部分当作文件名
  curl -O https://www.example.com/foo/bar.html
  上面命令将服务器回应保存成文件,文件名为bar.html
  
-s
  -s参数将不输出错误和进度信息
  curl -s https://www.example.com
  上面命令一旦发生错误,不会显示错误信息.不发生错误的话,会正常显示运行结果.
  如果想让curl不产生任何输出,可以使用以下的命令
  curl -s -o /dev/null https://google.com

-S
  -s参数指定只输出错误信息,通常于-s一起使用
  curl -s -o /dev/null https://googel.com
  上面命令没有任何输出,除非发生错误
  
-u
  -u参数用来设置服务器认证的用户名和密码
  curl -u 'bob:12345' https://google.com/login
  上面命令设置用户为bob,密码为12345 然后将其转为HTTP标头 Authorization: Basic 
  curl 能够识别URL里面的用户名和密码
  curl https://bob:12345@google.com/login
  上面命令能够识别设置了用户名,执行后,curl会提示用户输入密码
  
-v 
  -v参数输出通信的整个过程,用于调试
  curl -v https://www.example.com
  --trace 参数也可以用于调试,还会输出原始的二进制数据
  curl --trace - https://www.example.com
  
-x
  -x参数指定HTTP请求的代理
  curl -x socks5://james:[代理] https://www.example.com
  上面命令指定http请求通过[代理]的sock5代理发出
  如果没有指定代理协议,默认为http
  curl -x james:[代理] https://www.example.com
  上面命令中,请求的代理使用HTTP协议

-X 
  -X 参数指定HTTP请求的方法
  curl -X POST https://www.example.com
  上面命令对https://www.example.com发出POST请求
   
   
   
 详细来源:http://www.ruanyifeng.com/blog/2019/09/curl-reference.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值