curl下载安装与使用

下载:

https://curl.haxx.se/download.html

安装:

二进制安装。即解压即可。

使用

1、获取页面内容。

不加任何参数时,默认会发送GET请求来获取url内容到标准输出。

curl  url
D:AppCacheGolangHomesrclearn>curl www.apple.com

#=============================================================== D:AppCacheGolangHomesrclearn
>curl www.google.com curl: (52) Empty reply from server
#=============================================================== D:AppCacheGolangHomesrclearn
>curl www.xiaomi.com <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr/>Powered by MiWeb</body> </html>
#============================================================== D:AppCacheGolangHomesrclearn
>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.bdstati c.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 autofo cus></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 n ame=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/log in.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=b ri 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>
#===================================================================
D:AppCacheGolangHomesrclearn>

 分别测试了苹果、谷歌、小米、百度。发现获得的页面内容大小相差较大。苹果啥也不给,霸道。谷歌则给了一句温馨提示语。小米则简洁地回应了一个html,重定向了。百度则回应一堆信息。

2、获取http响应的头部信息。

D:AppCacheGolangHomesrclearn>curl -I http://www.apple.com
HTTP/1.1 301 Moved Permanently
Server: GHost
Content-Length: 0
Location: https://www.apple.com/
Cache-Control: max-age=0
Expires: Sun, 22 Sep 2019 04:59:29 GMT
Date: Sun, 22 Sep 2019 04:59:29 GMT
Connection: keep-alive
strict-transport-security: max-age=31536000
Set-Cookie: geo=CN; path=/; domain=.apple.com
Set-Cookie: ccl=shCgHjtuYBarTIycW/rvIg==; path=/; domain=.apple.com


#=================================================================== D:AppCacheGolangHomesrclearn
>curl -I http://www.google.com curl: (52) Empty reply from server
#==================================================================== D:AppCacheGolangHomesrclearn
>curl -I http://www.xiaomi.com HTTP/1.1 301 Moved Permanently Server: nginx Date: Sun, 22 Sep 2019 04:59:53 GMT Content-Type: text/html Content-Length: 223 Connection: close Location: https://www.mi.com
#=================================================================== D:AppCacheGolangHomesrclearn
>curl -I http://www.baidu.com HTTP/1.1 200 OK Server: bfe/1.0.8.18 Date: Sun, 22 Sep 2019 05:00:01 GMT Content-Type: text/html Content-Length: 277 Last-Modified: Mon, 13 Jun 2016 02:50:23 GMT Connection: Keep-Alive ETag: "575e1f6f-115" Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Pragma: no-cache Accept-Ranges: bytes
#========================================================================== D:AppCacheGolangHomesrclearn
>

这次谷歌给的响应头依然是一句简短的提示语,太节约资源了。其他三个则普通给出响应头,其中苹果和小米都重定向了。

4、同时给出http的响应头和响应体。

D:AppCacheGolangHomesrclearn>curl -i http://www.google.com
curl: (52) Empty reply from server

#=============================================================== D:AppCacheGolangHomesrclearn
>curl -i http://www.apple.com HTTP/1.1 301 Moved Permanently Server: GHost Content-Length: 0 Location: https://www.apple.com/ Cache-Control: max-age=0 Expires: Sun, 22 Sep 2019 05:10:26 GMT Date: Sun, 22 Sep 2019 05:10:26 GMT Connection: keep-alive strict-transport-security: max-age=31536000 Set-Cookie: geo=CN; path=/; domain=.apple.com Set-Cookie: ccl=Pnj18CxjBB4h7LQaKIIkxg==; path=/; domain=.apple.com
#===================================================================== D:AppCacheGolangHomesrclearn
>curl -i http://www.xiaomi.com HTTP/1.1 301 Moved Permanently Server: nginx Date: Sun, 22 Sep 2019 05:10:35 GMT Content-Type: text/html Content-Length: 223 Connection: close Location: https://www.mi.com <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr/>Powered by MiWeb</body> </html>
#============================================================= D:AppCacheGolangHomesrclearn
>curl -i http://www.baidu.com HTTP/1.1 200 OK Server: bfe/1.0.8.18 Date: Sun, 22 Sep 2019 05:10:47 GMT Content-Type: text/html Content-Length: 2381 Last-Modified: Mon, 23 Jan 2017 13:28:12 GMT Connection: Keep-Alive ETag: "588604ec-94d" Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Pragma: no-cache Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/ Accept-Ranges: bytes <!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.bdstati c.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 autofo cus></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 n ame=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/log in.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=b ri 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>
#====================================================
D:AppCacheGolangHomesrclearn>

谷歌依旧发挥着极简风格,就一句话。苹果给头不给体。小米则都给。百度则臃肿不堪。

原文地址:https://www.cnblogs.com/igoodful/p/11566963.html