本文的核心内容:curl命令。
在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。有时需要在容器内部请求接口,curl命令就可以很好的解决问题。
1. 获取页面内容
当我们不加任何选项使用 curl 时,默认会发送 GET 请求来获取链接内容到标准输出。
curl http://www.baidu.com
2. 显示 HTTP 请求 响应的头部信息 I
如果我们只想要显示 HTTP响应头部,而不显示文件内容,可以使用 -I 选项:
curl -I http://www.baidu.com
3. 显示 HTTP请求 响应的头部信息 + 页面内容 i
同时显示 HTTP 响应头部和文件内容,使用 -i 选项:
curl -i http://www.baidu.com
4. 显示HTTP请求 响应信息 + 页面内容
curl -v www.baidu.com
-v 选项,--verbose,指定该选项后,可以跟踪URL的连接信息。我们可以根据这个选项看看curl是怎么工作的。
域名解释过程:我们可以得到真正连接的IP地址和端口
请求头信息:其中有使用的协议(HTTP),协议的请求方式(GET)
回应头信息:包含状态码(200),内容格式(text/html),内容长度等。
* Rebuilt URL to: www.baidu.com/
* Trying 61.135.169.125...
* Connected to www.baidu.com (61.135.169.125) port 80 (#0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.49.0
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Content-Length: 2381
< Content-Type: text/html
< Date: Tue, 05 Mar 2019 03:07:19 GMT
< ETag: "588604c4-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:32 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< X-Cache: MISS from HexinFirewall
< Connection: close
<
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;cha
rset=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> <inpu
t type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <inp
ut type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=w
d 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://tie
ba.baidu.com name=tj_trtieba class=mnav>璐村惂</a> <noscript> <a href=http://www
.baidu.com/bdorz/login.gif?login&tpl=mn&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='+ encodeURI
Component(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bd
orz_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>©2017 Baidu <a href=http://www.baidu.com/duty/>浣跨敤
鐧惧害鍓嶅繀璇?/a> <a href=http://jianyi.baidu.com/ class=cp-feedback>鎰忚
鍙嶉</a> 浜琁CP璇?30173鍙?nbsp; <img src=//www.baidu.com/img/gs.gif> </
p> </div> </div> </div> </body> </html>
* Closing connection 0
5. 使用 -A 自定义 User-Agent
我们可以使用 -A 来自定义用户代理,例如下面的命令将伪装成安卓火狐浏览器对网页进行请求:
curl -A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" http://www.baidu.com
6. 使用 -H 自定义 header
当我们需要传递特定的 header 的时候,可以仿照以下命令来写:
curl -H "User-Agent: Custom-User-Agent" http://www.baidu.com
7. 使用 -L 自动跳转
当访问的网站返回一个新的跳转地址(重定向),使用-L自动跳转。
curl -L http://www.baidu.com
8. -X 选项,指定请求方式, -data选项,带参连接
请求的方式,包括GET、PUT、POST、DELETE四种方式。这四种请求方式,对于在做RESTFUL接口开发和测试的人来说,非常方便。
curl -X POST -data 'user=Maps&age=22' http://www.baidu.com
如果请求数据需要编码
curl -X POST -data-urlencode 'user=Maps&age=22' http://www.baidu.com
9. 保存网页内容
使用linux的重定向功能保存
curl https://www.baidu.com >> baidu.html
使用curl的内置option:-o(小写)保存网页
curl -o baidu.html https://www.baidu.com
本文详细介绍了curl命令在Linux系统中的应用,包括如何获取页面内容、显示HTTP请求响应的头部信息、自定义User-Agent和header、自动跳转、指定请求方式及保存网页内容。通过curl命令,用户可以在命令行下高效地进行HTTP请求。
19万+

被折叠的 条评论
为什么被折叠?



