命令-curl-强大的命令行 WEB 客户端

CURL 介绍

CURL,全称 Command Line URL Viewer,是一个 Linux 命令行工具( Windows 的 cmd 也有),能从服务器下载数据,也能往服务器上发送数据,支持多种协议,支持的协议有:DICT,FILE,FTP,FTPS,GOPHER,HTTP,HTTPS,IMAP,IMAPS,LDAP,LDAPS,POP3,POP3S,RTMP,RTSP,SCP,SFTP,SMB,SMBS,SMTP,SMTPS,TELNET和TFTP。
CURL 的功能非常强大,命令行选项几十种,如果使用熟练,完全可以替代 Postman 之类的工具。

  • 初识 curl 示例
[root@localhost tiger]# curl www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
[root@localhost tiger]# 

如上所示,在命令行执行命令 curl www.baidu.com ,就是向百度网站发了一个 GET 请求,把返回的 html 页面标准输出(在 windows 的 cmd 窗口执行会看到一样的效果),当然,返回的这一串东西如果在浏览器里就是美丽的百度首页。

CURL 命令选项简介

CURL 命令选项几十种,我也没有全部都会,这里介绍几种我比较熟悉的。也可以通过 curl --help 命令查看详细选项介绍:

[tiger@localhost tiger]$ curl --help
Usage: curl [options...] <url>
Options: (H) means HTTP/HTTPS only, (F) means FTP only
     --anyauth       Pick "any" authentication method (H)
 -a, --append        Append to target file when uploading (F/SFTP)
     --basic         Use HTTP Basic Authentication (H)
     --cacert FILE   CA certificate to verify peer against (SSL)
     --capath DIR    CA directory to verify peer against (SSL)
 -E, --cert CERT[:PASSWD] Client certificate file and password (SSL)
     --cert-type TYPE Certificate file type (DER/PEM/ENG) (SSL)
     --ciphers LIST  SSL ciphers to use (SSL)
     --compressed    Request compressed response (using deflate or gzip)
 ... ...
[tiger@localhost tiger$ 

这里并不详细介绍全部选项,所以没有展示全部的选项,做了省略。
根据这个帮助文档,这里做几点说明:

  1. curl 的命令选项分为长形式和短形式两种,短形式是为了输入方便,长形式更便于阅读,区别在于短形式单个字母左边一个中划线,长形式左边两个中划线。
  2. 帮助文档的第二行“Options: (H) means HTTP/HTTPS only, (F) means FTP only”,告诉我们 (H)标识的仅适用于 HTTP/HTTPS,而 (F) 标识的仅适用于 FTP。
  3. 命令行解析器总是解析整个命令行,因此,选项可以放在整个命令行的任何位置,当然是 curl 命令后边。

1、指定 user-agent

-A, --user-agent STRING  User-Agent to send to server (H)
[root@localhost tiger]# curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" www.baidu.com       
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@localhost tiger]# 

上边命令将请求百度的 User-Agent 改为 Chrome,不知道为什么这样请求百度不行。

2、指定向服务器发送的 Cookie

-b, --cookie STRING/FILE  String or file to read cookies from (H)
-c, --cookie-jar FILE  Write cookies to this file after operation (H)
     --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

-b 选项指定向服务器发送请求携带的 Cookie,
-c 将服务器响应的 Cookie 写入指定的本地文件

[root@localhost tiger]# curl -b "name=jiang" www.baidu.com
[root@localhost tiger]# curl -b "name=jiang;age=19"
[root@localhost tiger]# curl -b cookie.text www.baidu.com
[root@localhost tiger]# curl www.baidu.com -c cookie.text

3、向服务器发送 POST 请求

-d, --data DATA     HTTP POST data (H)
     --data-ascii DATA  HTTP POST ASCII data (H)
     --data-binary DATA  HTTP POST binary data (H)
     --data-urlencode DATA  HTTP POST data url encoded (H)
     --delegation STRING GSS-API delegation permission
     --digest        Use HTTP Digest Authentication (H)
     --disable-eprt  Inhibit using EPRT or LPRT (F)
     --disable-epsv  Inhibit using EPSV (F)

使用 -d 选型,会自动把请求类型转换为 POST 类型

[root@localhost tiger]# curl -d'login=jiang&password=123456'-X POST https://passport.csdn.net/v1/register/pc/login/doLogin
[root@localhost tiger]# curl -d 'login=jiang' -d 'password=123456' -X POST  https://passport.csdn.net/v1/register/pc/login/doLogin
[root@localhost tiger]# curl -d 'login=jiang' -d 'password=123456' https://passport.csdn.net/v1/register/pc/login/doLogin

4、指定 http 请求头

-H, --header LINE   Custom header to pass to server (H)
[root@localhost tiger]# curl www.baidu.com -H "Accept: application/json, text/javascript, */*; q=0.01"
[root@localhost tiger]# curl www.baidu.com -H "Accept: application/json, text/javascript, */*; q=0.01" -H "Content-Type: text/plain; charset=UTF-8"

-H 选项可以做的事情很多,比如说前边的 -A 选项指定 user-agent 也可以通过 -H 指定

[root@localhost tiger]# curl www.baidu.com -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"

5、显示打印服务器响应的 http 请求头

-i, --include       Include protocol headers in the output (H/F)
[root@localhost tiger]# curl www.baidu.com -i
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2381
Content-Type: text/html
Date: Sun, 03 May 2020 11:45:51 GMT
Etag: "588604c8-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
[root@localhost tiger]# 

6、保存服务器响应到文件

-o, --output FILE   Write output to <file> instead of stdout
     --pass PASS     Pass phrase for the private key (SSL/SSH)
     --post301       Do not switch to GET after following a 301 redirect (H)
     --post302       Do not switch to GET after following a 302 redirect (H)
     --post303       Do not switch to GET after following a 303 redirect (H)
-O, --remote-name   Write output to a file named as the remote file
     --remote-name-all Use the remote file name for all URLs

-o 选项将服务器的响应内容保存到指定文件,相当于 wget命令
-O 选项将服务器的响应内容保存本地文件,文件以 URL 的最后部分命名

[root@localhost tiger]# curl www.baidu.com -o baidu.html
[root@localhost tiger]# curl -o jd.html https://list.jd.com/list.html
[root@localhost tiger]# curl -O https://list.jd.com/list.html

7、指定请求的方式 GET、POST、DEL 等

-X, --request COMMAND  Specify request command to use
     --resolve HOST:PORT:ADDRESS  Force resolve of HOST:PORT to ADDRESS
     --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
[root@localhost tiger]# curl -O https://list.jd.com/list.html -X POST

常用请求方式:GET HEAD POST PUT DELETE

8、从浏览器复制 curl 命令

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9oSGow4F-1588508177278)(en-resource://database/3729:0)]
如图,以百度为例,F12 调出调试窗口,选择 Network 选项,点击百度一下,会看到发的请求在 Name 区域里,右键点击,选择 copy,选择 Copy as cURL(bash),可以直接在 linux 命令行粘贴执行,在文本编辑器粘贴,如下:在这里插入图片描述

curl 'https://www.baidu.com/s?ie=utf-8&mod=1&isbd=1&isid=9d2c454c00268b98&ie=utf-8&f=8&rsv_bp=1&tn=02003390_42_hao_pg&wd=%E8%AF%B7%E6%B1%82%E7%B1%BB%E5%9E%8B&oq=%25E8%25AF%25B7%25E6%25B1%2582%25E7%25B1%25BB%25E5%259E%258B&rsv_pq=9d2c454c00268b98&rsv_t=8e8dR0g7iwgAng3sCvw9IFvmERM648PEcsgDIVNh1VgulkPsl%2FwPUJDT8LedH3e6ey0fz9%2F3G3a3&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t&bs=%E8%AF%B7%E6%B1%82%E7%B1%BB%E5%9E%8B&rsv_sid=undefined&_ss=1&clist=539a118df1a45064&hsug=&f4s=1&csor=4&_cr1=33610' \
  -H 'Connection: keep-alive' \
  -H 'Accept: */*' \
  -H 'is_xhr: 1' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -H 'is_referer: https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=02003390_42_hao_pg&wd=%E8%AF%B7%E6%B1%82%E7%B1%BB%E5%9E%8B&oq=%25E8%25AF%25B7%25E6%25B1%2582%25E7%25B1%25BB%25E5%259E%258B&rsv_pq=cb1c27b2002656de&rsv_t=d39b42BaEXsGScEwboLzHLcZH1L3H1SdSJwMYOP02CXHreEriKniE0gRVPDXHwzhovT%2FPYWIdhJA&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t&bs=%E8%AF%B7%E6%B1%82%E7%B1%BB%E5%9E%8B' \
  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36' \
  -H 'is_pbs: %E8%AF%B7%E6%B1%82%E7%B1%BB%E5%9E%8B' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Referer: https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=02003390_42_hao_pg&wd=%E8%AF%B7%E6%B1%82%E7%B1%BB%E5%9E%8B&oq=%25E8%25AF%25B7%25E6%25B1%2582%25E7%25B1%25BB%25E5%259E%258B&rsv_pq=9d2c454c00268b98&rsv_t=8e8dR0g7iwgAng3sCvw9IFvmERM648PEcsgDIVNh1VgulkPsl%2FwPUJDT8LedH3e6ey0fz9%2F3G3a3&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t' \
  -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8' \
  -H 'Cookie: BIDUPSID=61A77E9F80FF6E2CA97CDB58F4990350; PSTM=1585648123; BD_UPN=12314753; BAIDUID=6D0CF462C6992B07B54E2FDC3A27BA3D:FG=1; sugstore=0; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; H_PS_PSSID=; H_PS_645EC=8e8dR0g7iwgAng3sCvw9IFvmERM648PEcsgDIVNh1VgulkPsl%2FwPUJDT8LedH3e6ey0fz9%2F3G3a3; delPer=0; BD_CK_SAM=1; PSINO=1; BDSVRTM=78; WWW_ST=1588507628437' \
  --compressed

CURL 的介绍就这样,以后用到了回来补充。在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值