blog icon indicating copy to clipboard operation
blog copied to clipboard

cURL 的使用

Open penglongli opened this issue 7 years ago • 0 comments
trafficstars

cUrl 常用参数如下所示:

  • -L 参数

    当所要访问的 URL 被转移(重定向)到了其它的 URL,这个参数能够让 cURL 对新 URL 执行

    $ curl -L http://v2ex.com
    

    简单理解,类似于浏览器上输入 http://v2ex.com 会被重定向至 https://www.v2ex.com 。这个重定向一般是站点 Nginx 设置的

  • -i / -I 参数 -I 参数通常与 -v 参数结合使用,不会输出源码

$ curl -vI v2ex.com
* Rebuilt URL to: v2ex.com/
*   Trying 183.60.90.88...
* TCP_NODELAY set
* Connected to v2ex.com (183.60.90.88) port 80 (#0)
> HEAD / HTTP/1.1
> Host: v2ex.com
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
HTTP/1.1 301 Moved Permanently
< Server: nginx/1.13.3
Server: nginx/1.13.3
< Date: Wed, 10 Jan 2018 13:58:45 GMT
Date: Wed, 10 Jan 2018 13:58:45 GMT
< Content-Type: text/html
Content-Type: text/html
< Content-Length: 185
Content-Length: 185
< Connection: keep-alive
Connection: keep-alive
< Location: https://v2ex.com/
Location: https://v2ex.com/
< X-ORCA-Accelerator:  from 093.chn.can02.cn.krill.zenlogic.net
X-ORCA-Accelerator:  from 093.chn.can02.cn.krill.zenlogic.net
< X-Cache:  from 093.chn.can02.cn.krill.zenlogic.net
X-Cache:  from 093.chn.can02.cn.krill.zenlogic.net

<
* Connection #0 to host v2ex.com left intact

-i 参数会打印出来请求的 headers 信息

$ curl -i http://v2ex.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.11.9
Date: Wed, 13 Dec 2017 14:37:04 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://v2ex.com/
X-ORCA-Accelerator:  from k02.chn.szx01.cn.krill.zenlogic.net
X-Cache:  from k02.chn.szx01.cn.krill.zenlogic.net

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.11.9</center>
</body>
</html>

如上代码所示,请求 http://v2ex.com 会发现被 301 到了 https://v2ex.com/

还有个更有意思的事情,如果我们继续做操作

$ curl -i https://v2ex.com
HTTP/2 301
server: nginx/1.11.9
date: Wed, 13 Dec 2017 14:39:09 GMT
content-type: text/html
content-length: 185
location: https://www.v2ex.com/
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-orca-accelerator:  from k02.chn.szx01.cn.krill.zenlogic.net
x-cache:  from k02.chn.szx01.cn.krill.zenlogic.net

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.11.9</center>
</body>
</html>

会发现,这个站点的 https://v2ex.com 会被 301 到 https://www.v2ex.com

  • -v 参数

    使用 -v 参数能够拿到更详细的信息,通常与 -i 参数共用。对于调试很有用处

    $ curl -v http://v2ex.com
    * Rebuilt URL to: http://v2ex.com/
    *   Trying 121.14.111.19...
    * TCP_NODELAY set
    * Connected to v2ex.com (121.14.111.19) port 80 (#0)
    > GET / HTTP/1.1
    > Host: v2ex.com
    > User-Agent: curl/7.54.0
    > Accept: */*
    >
    < HTTP/1.1 301 Moved Permanently
    < Server: nginx/1.11.9
    < Date: Wed, 13 Dec 2017 14:45:58 GMT
    < Content-Type: text/html
    < Content-Length: 185
    < Connection: keep-alive
    < Location: https://v2ex.com/
    < X-ORCA-Accelerator:  from k02.chn.szx01.cn.krill.zenlogic.net
    < X-Cache:  from k02.chn.szx01.cn.krill.zenlogic.net
    <
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body bgcolor="white">
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center>nginx/1.11.9</center>
    </body>
    </html>
    * Connection #0 to host v2ex.com left intact
    

    > 表示被 curl 发送的 header data

    < 表示被 curl 接收到的 header data

  • —trace 参数

    这个参数能拿到更详细的信息,一般来说并不是很经常用到:

    $ curl --trace curl.out https://www.baidu.com
    
  • -x 参数

    此参数用来设置代理,支持 socks4、socks4a、socks5、socks5h、http。

    我在 SSH 使用 中说的通过动态端口转发翻墙用到的就是 socks5 代理

    使用方法: -x, --proxy <[protocol://][user:password@]proxyhost[:port]>

    $ ssh -D 4444 [email protected]
    # 103.143.242.122 是一台香港机器
    $ curl -x socks5://localhost:4444 https://www.google.com.hk
    # 这样使用我们就可以通过代理访问 https://www.google.com.hk
    
    注意:此处未按照上述使用 user:password 是因为我本机的公钥已经加入到了香港机器的授权中
    
  • -X 参数

    此参数我们通常用来发送 http 请求。

    1. 发送 POST 请求:
    $ curl -H "Content-Type: application/json" -X POST -d '{"key":"value"}' https://test.com/form
    
  • --limit-rate-# 参数

    --limit-rate 能够限制下载速度,-# 显示进度条

    $ curl --limit-rate 1234B -O -# https://www.computerhope.com/index.htm
    
  • -m 参数 链接超时时间设置

    $ curl -m 1 v2ex.com
    

应用

输出状态码

$ curl -s -o /dev/null -I -w "%{http_code}" http://www.example.org
200
$ curl -s -o /dev/null -I -w "%{http_code}" http://www.v2ex.com
301

penglongli avatar Dec 05 '17 11:12 penglongli