命令事例
发送POST请求:
如果传输文件:curl -F "blob=@tmp.txt;type=text/plain" localhost:8080/request_body
普通post请求:curl -d "method=searchone&module=seller&user_name=wb-liqiu&nickname=dd" -H"Host:fmp.view.lz.taobao.com" "10.235.160.141:8082/api.php"
模拟登录,传输cookie和参数:
下载cookie,命令:curl -c ./cookie.txt -d "language=zh&password=admin&username=admin" http://***.com:8080/saiku/rest/saiku/session
携带cookie以登录用户身份访问,命令如下:curl -b ./cookie.txt -L -d "name=/homes/test2&file=/homes/search.saiku" -H"Host:***.com" "192.0.0.134:8080/saiku/rest/saiku/api/repository/resource"
伪造来源地址,有的网站会判断,请求来源地址
curl -e http://localhost http://www.sina.com.cn
伪造cookie,防止302跳转页面不能下载
wget --cookies=on --load-cookies=cookie.txt --keep-session-cookies --save-cookies=cookie.txt http://detail.tmall.com/item.htm?id=25139372969
使用proxy缓存
curl -L -x127.0.0.1:13128 http://detail.tmall.com/item.htm?id=25823396605
命令事例详解
一、查看网页源码
直接在curl命令后加上网址,就可以看到网页源码。我们以网址www.sina.com为例(选择该网址,主要因为它的网页代码较短)
curl www.baidu.com
如果要把这个网页保存下来,可以使用-o参数,这就相当于使用wget命令了。
curl -o [文件名] www.baidu.com
二、自动跳转 有的网址是自动跳转的。使用-L参数,curl就会跳转到新的网址。curl -L http://item.taobao.com/item.htm?id=25823396605 键入上面的命令,结果就自动跳转为http://detail.tmall.com/item.htm?id=25823396605
三、显示头信息 -i 参数可以显示http response的头信息,连同网页代码一起。(-I 参数则是只显示http response的头信息。)
curl -i www.baidu.com
HTTP/1.1 200OK
Date: Fri,28 Feb 2014 05:39:57GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: Keep-Alive
Vary: Accept-Encoding
Set-Cookie: BAIDUID=0F251A658E427EBB7CBEB0C3F4A70FAE:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; d
omain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/Set-Cookie: H_PS_PSSID=4104_5231_1445_5139_5225_5378_5368_4261_4760_5400; path=/; domain=.baidu.com
P3P: CP="OTI DSP COR IVA OUR IND COM"Expires: Fri,28 Feb 2014 05:39:45GMT
Cache-Control: privateServer: BWS/1.1BDPAGETYPE:1BDQID:0xc3b306dca955703dBDUSERID:0
.....
四、显示通信过程 -v 参数可以显示一次http通信的整个过程,包括端口连接和http request头信息。命令:curl -v www.baidu.com
* About to connect() to www.baidu.com port 80
* Trying 115.239.210.26... connected* Connected to www.baidu.com (115.239.210.26) port 80
> GET / HTTP/1.1
> User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
>Host: www.baidu.com> Accept: */*>
< HTTP/1.1 200 OK
< Date: Fri, 28 Feb 2014 05:42:37 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: Keep-Alive
< Vary: Accept-Encoding
< Set-Cookie: BAIDUID=442AD49501EF253AE71F2BAF3E0181FB:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BDSVRTM=0; path=/
< Set-Cookie: H_PS_PSSID=5228_1461_5187_5138_5225_5379_5368_4261_4760_5401_5286; path=/; domain=.baidu.com
< P3P: CP=" OTI DSP COR IVA OUR IND COM "
< Expires: Fri, 28 Feb 2014 05:41:43 GMT
< Cache-Control: private
< Server: BWS/1.1
< BDPAGETYPE: 1
< BDQID: 0x906950d16fb1e95d
< BDUSERID: 0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Spee
="X-UA-Compatible" content="IE=Edge">
六、文件上传 假定文件上传的表单你可以用curl这样上传文件,命令:curl --form upload=@localfilename --form press=OK http://www.baidu.com
七、Referer字段 有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的: curl --referer http://www.baidu.com http://www.baidu.com
八、User Agent字段 这个字段是用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同设备,返回不同格式的网页,比如手机版和桌面版。 iPhone4的User Agent是: “Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7”。curl可以这样模拟: curl --user-agent "[User Agent]" [URL]
九、cookie 使用--cookie参数,可以让curl发送cookie。curl --cookie "name=xxx" www.baidu.com 至于具体的cookie的值,可以从http response头信息的Set-Cookie字段中得到。也可以例如:curl -c ./cookie.txt http://www.baidu.com
十、增加头信息 有时需要在http request之中,自行增加一个头信息。--header参数就可以起到这个作用。curl --header "xxx: xxxxxx" http://baidu.com
十一、HTTP认证 有些网域需要HTTP认证,这时curl需要用到--user参数:curl --user name:password baidu.com
十二、设置代理缓存-x
curl -L -x127.0.0.1:13128 http://detail.tmall.com/item.htm?id=25823396605