curl是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称url为下载工具。
1. 获取网页
curl www.example.com
该命令将下载整个HTML文档。默认不显示文档头部,即html的header。要全部显示,加-i参数,可以显示http response的头信息,连同网页代码一起;只显示http response的头信息,用参数-I。 在任何时候,都可以使用-v参数跟踪curl的运行过程,可以显示一次http通信的整个过程,包括端口连接和http request头信息。
-i参数:
curl -i www.sina.com
HTTP/1.0 301 Moved Permanently
Date: Sat, 01 Dec 2012 04:37:47 GMT
Server: Apache
Location: http://www.sina.com.cn/
Cache-Control: max-age=3600
Expires: Sat, 01 Dec 2012 05:37:47 GMT
Vary: Accept-Encoding
Content-Length: 231
Content-Type: text/html; charset=iso-8859-1
Age: 201
X-Cache: HIT from xd33-75.sina.com.cn
Connection: close
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
</body></html>
-I参数:
curl -I www.sina.com
HTTP/1.0 301 Moved Permanently
Date: Sat, 01 Dec 2012 04:00:35 GMT
Server: Apache
Location: http://www.sina.com.cn/
Cache-Control: max-age=3600
Expires: Sat, 01 Dec 2012 05:00:35 GMT
Vary: Accept-Encoding
Content-Length: 231
Content-Type: text/html; charset=iso-8859-1
Age: 2676
X-Cache: HIT from xd33-97.sina.com.cn
Connection: close
-v参数:
curl -v www.sina.com
* About to connect() to www.sina.com port 80 (#0)
* Trying 202.108.33.85... connected
* Connected to www.sina.com (202.108.33.85) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.17.0 (i586-pc-mingw32msvc) libcurl/7.17.0 zlib/1.2.2
> Host: www.sina.com
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 301 Moved Permanently
< Date: Sat, 01 Dec 2012 04:45:21 GMT
< Server: Apache
< Location: http://www.sina.com.cn/
< Cache-Control: max-age=3600
< Expires: Sat, 01 Dec 2012 05:45:21 GMT
< Vary: Accept-Encoding
< Content-Length: 231
< Content-Type: text/html; charset=iso-8859-1
< Age: 120
< X-Cache: HIT from xd33-85.sina.com.cn
< Connection: close
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
</body></html>
* Closing connection #0
如果你觉得上面的信息还不够,那么下面的命令可以查看更详细的通信过程。
curl --trace output.txt www.sina.com
或者
curl --trace-ascii output.txt www.sina.com
运行后,请打开output.txt文件查看。
2. 保存网页
curl www.example.com > index.html
或使用-o参数:
curl -o index.html www.example.com
3. 自动跳转
有的网址是自动跳转的。使用-L参数,curl就会跳转到重定向新的网址。
curl -L www.example.com
4.发送表单信息
发送表单信息有GET和POST两种方法。GET方法相对简单,只要把数据附在网址后面就行。
curl example.com?data=xxx
POST方法必须把数据和网址分开,curl就要用到--data参数。
curl --data "data=xxx" example.com/from
如果你的数据没有经过表单编码,还可以让curl为你编码,参数是--data-urlencode。
curl --data-urlencode "date=xxx" example.com/form
使用-X参数可以支持其他POST,DELETE等动词。
curl -X POST www.example.com
curl -X DELETE www.example.com
5.文件上传
假定文件上传的表单是下面这样:
<form method="POST" enctype="multipart/form-data" action="upload">
<input type="file" name="upload">
<input type="submit" name="submit" value="OK">
</form>
curl这样上传文件:
curl --form upload=@localfilename --form press=OK www.example.com
6.增加头信息
有时需要在http request之中,自行增加一个头信息。--header参数就可以起到这个作用。
curl --header "xxx: xxxxxx" http://www.example.com
如添加
User Agent字段
这个字段是用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同设备,返回不同格式的网页,比如手机版和桌面版。
添加Referer信息,伪装从某网站过来
curl --header "Referer:www.index.com User-Agent:xxx" www.example.com