Linux、Unix 上 Curl 的常用用法
1. 访问网站
直接抓取 html 输出在终端
curl http://cip.cc
跟随网站的 301 跳转
curl -L http://cip.cc
显示头部与内容
curl -i http://www.codebelief.com
只显示头部
curl -i http://www.codebelief.com
自定义 User-Agent
curl -A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" http://www.baidu.com
自定义头部 Header
curl -H "Referer: www.example.com" -H "User-Agent: Custom-User-Agent" http://www.baidu.com
2. 下载文件
将网站文件保存为 my.html
curl -o my.html baidu.com curl http://baidu.com > test.html
将文件保存为默认文件 index.html
curl -O baidu.com
同时下载两个文件
curl -O baidu.com -O http://hao123.com
这样就会把 dodo1,dodo2,dodo3,dodo4,dodo5 全部保存下来
http://www.linux.com/dodo[1-5].jpg
断点下载,网络中断后可以用这样恢复
curl -C - -O http://baidu.com
下载速度最大不会超过 1000KB/second
curl --limit-rate 1000KB -O http://www.baidu.com
若 index.html 文件在 2011/12/21 之后有过更新才会进行下载
curl -z 21-Dec-11 http://www.baidu.com
3. 授权(开发时,一般给 API 提供 http-basic-auth)
根据提示填写密码
curl -u username:password Url curl -u username url
4. 从 FTP 服务器下载文件
列出 downLoad 文件夹下所有的文件,并非下载
curl -u ftpUser:ftpPass -O ftp://ftp_server_url/downLoad/
下载 xxx.php 文件
curl -u ftpUser:ftpPass -O ftp://ftp_server_url/downLoad/xxx.php
是不是和 scp 有点像
curl -O ftp://ftpUser:[email protected]_server_url/xxxx.JPG
5. 上传到 FTP 服务器
将 myfile.txt 文件上传到服务器
curl -u ftpUser:ftpPass -T test.html ftp://ftp.testserver.com
同时上传多个文件
curl -u ftpUser:ftpPass -T "{file,file}" ftp://ftp.testserver.com
從標準輸入獲取內容儲存到伺服器指定的檔案中
curl -u ftpUser:ftpPass -T - ftp://ftp.testserver.com/myfile_1.txt
6. 设置代理访问
使用的是 http、https 代理
curl -x 127.0.0.1:3128 http://google.co.in
使用 sock5 代理
curl --socks5 127.0.0.1:1080 http://cip.cc
7. 使用 Cookie
将网站的 cookies 信息保存到 cookieFile 文件中
curl -D cookieFile http://localhost/sugarcrm/index.php
使用上次保存的 cookie 信息
curl -b cookieFile http://localhost/sugarcrm/index.php
使用 -c 保存 Cookie
curl -c "cookie-example" http://www.example.com
使用 -b 导入 cookie
curl -b "JSESSIONID=D0112A5063D938586B659EF8F939BE24" http://www.example.com curl -b "cookie-example" http://www.example.com
使用 Cookie 登录
curl -c "cookie-login" -d "userName=test&passwd=test" http://www.example.com/login curl -b "cookie-login" http://www.example.com/login
8. 请求数据
GET 请求
curl -u username https://api.github.com/user?access_token=XXXXXXXXXX
POST 请求 (特殊字符需要进行转义)
curl -u username --data "param1=value1¶m2=value" https://api.github.com curl --data-urlencode "value 1" http://hostname.com ### url encode auto
指定其他 协议
curl -I -X DELETE https://api.github.cim
-X POST 是指定请求方式,如果没指定,默认是 POST
9. 伪造 referer(盗链)
以为你是从 www.linux.com 跳转过去
curl -e "www.linux.com" http://mail.linux.com