[Linux] curl命令

1. curl 获取页面内容
2. curl -v 显示通信过程
3. curl --trace 显示详细通信过程
4. curl -I (大写的i)显示 HTTP 头
5. curl -o 和 curl -O 保存到文件
6. curl -L 跟随链接重定向

使用 -H 自定义 header
使用 -c 保存 Cookie
使用 -b 读取 Cookie
使用 -d 发送 POST 请求(-X 指定发送数据的方式)

------------------------------------------------------------------

1. curl 获取页面内容
[例] curl http://www.example.com/
执行结果:

[root@xxx ~]# curl http://www.example.com/
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
[root@xxx ~]#

2. curl -v 显示通信过程
[例] curl -v http://www.example.com/
执行结果:

[root@xxx ~]# curl -v http://www.example.com/
* About to connect() to www.example.com port 80 (#0)
*   Trying 2606:2800:220:1:248:1893:25c8:1946...
* Connected to www.example.com (2606:2800:220:1:248:1893:25c8:1946) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.example.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Cache-Control: max-age=604800
< Content-Type: text/html; charset=UTF-8
< Date: Sat, 11 Jan 2020 03:23:05 GMT
< Etag: "3147526947+ident"
< Expires: Sat, 18 Jan 2020 03:23:05 GMT
< Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
< Server: ECS (dcb/7EEF)
< Vary: Accept-Encoding
< X-Cache: HIT
< Content-Length: 1256
<
<!doctype html>
<html>
(省略...)
</html>
* Connection #0 to host www.example.com left intact
[root@xxx ~]#

通信过程(2-4行)+ 请求头信息(5-8行)+ 响应头信息(10-20行)+ 网页代码

如果请求的是https开头的完整网址,除了以上还会显示网站的安全证书信息

[例] curl -v -I https://www.runoob.com/js/js-tutorial.html
执行结果:网站的安全证书信息(5-14行)

[root@xxx ~]# curl -v -I https://www.runoob.com/js/js-tutorial.html
* About to connect() to www.runoob.com port 443 (#0)
*   Trying 111.41.52.116...
* Connected to www.runoob.com (111.41.52.116) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*       subject: CN=www.runoob.com
*       start date: Sep 27 00:00:00 2019 GMT
*       expire date: Sep 26 12:00:00 2020 GMT
*       common name: www.runoob.com
*       issuer: CN=Encryption Everywhere DV TLS CA - G1,OU=www.digicert.com,O=DigiCert Inc,C=US
> HEAD /js/js-tutorial.html HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.runoob.com
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
(省略...)

3. curl --trace 显示详细通信过程

[例] curl --trace ./example.txt http://www.example.com/ , trace信息会保存至example.txt

[root@xxx ~]# curl --trace ./example.txt http://www.example.com/
<!doctype html>
<html>
<head>
  (省略...)
</head>

<body>
  (省略...)
</body>
</html>
[root@xxx ~]#
[root@xxx ~]# cat example.txt
== Info: About to connect() to www.example.com port 80 (#0)
== Info:   Trying 93.184.216.34...
== Info: Connected to www.example.com (93.184.216.34) port 80 (#0)
=> Send header, 79 bytes (0x4f)
0000: 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1..
0010: 55 73 65 72 2d 41 67 65 6e 74 3a 20 63 75 72 6c User-Agent: curl
0020: 2f 37 2e 32 39 2e 30 0d 0a 48 6f 73 74 3a 20 77 /7.29.0..Host: w
0030: 77 77 2e 65 78 61 6d 70 6c 65 2e 63 6f 6d 0d 0a ww.example.com..
0040: 41 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a    Accept: */*....
<= Recv header, 17 bytes (0x11)
0000: 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d HTTP/1.1 200 OK.
0010: 0a                                              .
<= Recv header, 22 bytes (0x16)
(省略...)
<= Recv data, 154 bytes (0x9a)
0000: 70 72 69 6f 72 20 63 6f 6f 72 64 69 6e 61 74 69 prior coordinati
0010: 6f 6e 20 6f 72 20 61 73 6b 69 6e 67 20 66 6f 72 on or asking for
0020: 20 70 65 72 6d 69 73 73 69 6f 6e 2e 3c 2f 70 3e  permission.</p>
0030: 0a 20 20 20 20 3c 70 3e 3c 61 20 68 72 65 66 3d .    <p><a href=
0040: 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 69 61 6e "https://www.ian
0050: 61 2e 6f 72 67 2f 64 6f 6d 61 69 6e 73 2f 65 78 a.org/domains/ex
0060: 61 6d 70 6c 65 22 3e 4d 6f 72 65 20 69 6e 66 6f ample">More info
0070: 72 6d 61 74 69 6f 6e 2e 2e 2e 3c 2f 61 3e 3c 2f rmation...</a></
0080: 70 3e 0a 3c 2f 64 69 76 3e 0a 3c 2f 62 6f 64 79 p>.</div>.</body
0090: 3e 0a 3c 2f 68 74 6d 6c 3e 0a                   >.</html>.
== Info: Connection #0 to host www.example.com left intact
[root@xxx ~]#

4. curl -I 显示 HTTP 头

如果只想要显示 HTTP 头,而不显示文件内容,可以使用 -I 选项
[例] curl -I http://www.baidu.com
执行结果:

[root@xxx ~]# curl -I www.baidu.com
HTTP/1.1 200 OK
Bdpagetype: 1
Bdqid: 0xe01e17b10004c95e
Cache-Control: private
Connection: keep-alive
Content-Type: text/html
Cxy_all: baidu+5e3595f77801cea516a9fe717f733f66
Date: Sat, 11 Jan 2020 02:47:02 GMT
Expires: Sat, 11 Jan 2020 02:46:30 GMT
Server: BWS/1.1
Set-Cookie: BIDUPSID=5e3f0bf4d2996c34cbe60a98745e3ff0; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1578710822; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: delPer=0; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=1445_21079_30210_30491_26350; path=/; domain=.baidu.com
Set-Cookie: BAIDUID=5e3f0bf4d2996c34cbe60a98745e3ff0:FG=1; Path=/; Domain=baidu.com; Max-Age=31536000
Traceid: 1578710822069074202616149371362774141278
Vary: Accept-Encoding
X-Ua-Compatible: IE=Edge,chrome=1

[root@xxx ~]#

同时显示 HTTP 头和文件内容,使用 -i 选项

[root@xxx ~]# curl -i http://www.example.com/
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sat, 11 Jan 2020 03:29:24 GMT
Etag: "3147526947"
Expires: Sat, 18 Jan 2020 03:29:24 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (sjc/4E74)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
[root@xxx ~]#

5. curl -o 和 curl -O 保存到文件

-o:结果会被保存到命令行中提供的文件名
-O:URL 中的文件名会被用作保存输出的文件名
注意:使用 -O 选项时,必须确保链接末尾包含文件名,否则 curl 无法正确保存文件

6. curl -L 跟随链接重定向
[例] 关于-o、-O 和 -L
测试页面:
https://www.runoob.com/js 
https://www.runoob.com/js/js-tutorial.html
在浏览器地址栏输入https://www.runoob.com/js ,会重定向到https://www.runoob.com/js/js-tutorial.html

(1)curl https://www.runoob.com/js  没有返回结果

[root@xxx ~]# curl https://www.runoob.com/js
[root@xxx ~]#

(2)curl -o js.html -L https://www.runoob.com/js  加上-L会重定向到 js-tutorial.html,文件成功保存为js.html

[root@xxx ~]# curl -o js.html -L https://www.runoob.com/js
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 60050  100 60050    0     0   176k      0 --:--:-- --:--:-- --:--:-- 1123k
[root@xxx ~]#

(3)curl -o js-t.html https://www.runoob.com/js/js-tutorial.html,直接保存该页面与上条笔记,下载的文件一样

[root@xxx ~]# curl -o js-t.html https://www.runoob.com/js/js-tutorial.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 60050  100 60050    0     0   223k      0 --:--:-- --:--:-- --:--:--  222k
[root@xxx ~]# diff js.html js-t.html
[root@xxx ~]#

(4)curl -O https://www.runoob.com/js/js-tutorial.html,-O保存的文件名默认为js-tutorial.html。以上方式下载的是相同页面

[root@xxx ~]# curl -O https://www.runoob.com/js/js-tutorial.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 60050  100 60050    0     0   217k      0 --:--:-- --:--:-- --:--:--  217k
[root@xxx ~]#
[root@xxx ~]# diff js-t.html js-tutorial.html
[root@xxx ~]# ls -l js*
-rw-r--r--. 1 root root 60050 Jan 11 10:59 js.html
-rw-r--r--. 1 root root 60050 Jan 11 10:59 js-t.html
-rw-r--r--. 1 root root 60050 Jan 11 11:00 js-tutorial.html
[root@xxx ~]#

curl命令的用法参考资料:
https://blog.csdn.net/jdbdh/article/details/90911383
https://blog.csdn.net/Charliewolf/article/details/83542198
https://blog.csdn.net/Chenliaoyuanjv/article/details/79689028

发布了89 篇原创文章 · 获赞 1 · 访问量 4828

猜你喜欢

转载自blog.csdn.net/wy_hhxx/article/details/103938689
今日推荐