0 前言
curl是Linux中的一款综合性网络传输工具,既可以上传也可以下载,支持HTTP、HTTPS、FTP等30余种常见协议。
该命令选项超多,在学习笔记68中,我们列举了该命令的部分实例,今天继续通过实例来研究curl命令的功能和用法。
1 curl命令应用实例
1.1 跟随重定向:curl -L 统一资源定位符
在访问一个网页时,如果这个网页已经移动到另一个站点时,会发送一个HTTP Loaction header作为请求,然后将请求重定向到新的地址上。
然而curl在默认情况下不会发送HTTP Location headers(重定向),这时我们可以指定 -L 选项来跟随重定向。
例如,我们打开g.cn时会自动重定向跳转到 google.cn。
当我们使用命令 curl g.cn时,看到的是g.cn的代码。
要想看到google.cn的代码,可以在命令中指定-L选项,即 curl -L g.cn
purpleEndurer @ bash ~] curl g.cn
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://google.cn/">here</A>.
</BODY></HTML>
[purpleEndurer @ bash ~] curl -L g.cn
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>Google</title>
<style>
html { background: #fff; margin: 0 1em; }
body { font: .8125em/1.5 arial, sans-serif; text-align: center; }
h1 { font-size: 1.5em; font-weight: normal; margin: 1em 0 0; }
p#footer { color: #767676; font-size: .77em; }
p#footer a { background: url(//www.google.cn/intl/zh-CN_cn/images/cn_icp.gif) top right no-repeat; padding: 5px 20px 5px 0; }
ul { margin: 2em; padding: 0; }
li { display: inline; padding: 0 2em; }
div { -moz-border-radius: 20px; -webkit-border-radius: 20px; border: 1px solid #ccc; border-radius: 20px; margin: 2em auto 1em; max-width: 650px; min-width: 544px; }
div:hover, div:hover * { cursor: pointer; }
div:hover { border-color: #999; }
div p { margin: .5em 0 1.5em; }
img { border: 0; }
</style>
</head>
<body>
<div>
<a href="https://www.google.com.hk/webhp?hl=zh-CN&sourceid=cnhp">
<img src="//www.google.cn/intl/zh-CN_cn/landing/cnexp/google-search.png" alt="Google" width="586" height="257">
</a>
<h1><a href="https://www.google.com.hk/webhp?hl=zh-CN&sourceid=cnhp"><strong id="target">google.com.hk</strong></a></h1>
<p>请收藏我们的网址
</div>
<p id="footer">
<span>ICP证合字B2-20070004号</span>
</p>
</body>
</html>
[purpleEndurer @ bash ~]
1.2 断点续传:curl -C - -O 统一资源定位符
在Windows中,我们可以使用迅雷等的软件进行断点续传。
在Linux中,curl可以通过-C选项同样可以达到相同的断点续传效果。
我们以下载 QQ Linux版 文件