【curl】 常用总结

curl 在测试http 请求非常有用,可以访问下载,也可以模拟浏览器,支持多种协议, 是个必备的技能

参数

  1. -A 添加ua

  2. -b filename 读取cookie的地方

  3. -o filename 把网页保存到文件中

  4. -d data POST请求参数

  5. -I 只查看请求头信息

  6. -v 显示通信过程

  7. -H 自定义请求头

  8. -s 静默模式,不显示什么信息

  9. -w "xxxx" 请求完成后打印信息

  10. -x host:port 使用代理

  11. -c 保存cookie的地方

  12. -r 支持断点续传

  13. -L 跟踪重定向

  14. -0 或者 --http1.0 使用http1.0

  15. -r range请求

案例

对照上面的请求参数,理解下面常用的例子,大部分的场景都能覆盖(很多场景有几种写法,这里没有列全,可以curl -h 自行补充)

这里基本都是开发用到的,curl远不止这些功能,还有代理,SSL, 限速,文件上传等等非常多的功能

下载一个网页

  1. curl http://baidu.com > baidu.com

  2. curl -o baidu.com http://baidu.com

模拟get请求

  1. curl http://baidu.com/?wd=curl

模拟post请求

  1. curl -d "pass=cc&name=vv" http://baidu.com

只看请求头

  1. curl -I http://baidu.com

查看通信过程

  1. curl -v orangleliu.info

自定义请求头

  1. curl -I -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" www.baidu.com

  2. curl --user-agent "[User Agent]" -I baidu.com

自定义reffer

  1. curl -I -e i-erya.com baidu.com

请求使用gzip

  1. curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte

测试连接网站的速度

  1. curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.baidu.com"

断点续传 下载

  1. curl -C - -O http://www.gnu.org/software/gettext/manual/gettext.html

GET方法 http1.0版本

  1. curl -X GET -0 -I 'http://124.88.52.98:3698/account/ulogin/4/?wlanuserip=10.14.186.63&wlanacname=&ssid=eWiFi-JS&wlanparameter=68-96-7b-ef-f5-72'

basic auth 自定义header

  1. curl -u sso:sso -H 'Host:api.sso.modocker.com' 'http://47.93.239.182/api/wechat/qrcode' -i

curl 模拟 multipart/form-data 表单

注意每个k v都要用一个 -F

  1. curl -v -X post -F "data=@dump.rdb" -F "bucket=test" -F "key=test1" -F "md5=xxx" 'http://localhost:5003/upload/key'

多个文件上传

  1. $ curl -F 'fileX=@/path/to/fileX' -F 'fileY=@/path/to/fileY' ... http://localhost/upload

上传一组文件,也是多个文件上传

  1. $ curl -F 'files[]=@/path/to/fileX' -F 'files[]=@/path/to/fileY' ... http://localhost/upload

PUT 方式上传文件

  1. curl -v --upload-file dump.rdb 'http://localhost:5003/upload/key'

发送jsonin 请求

  1. $ curl --header "Content-Type: application/json" \

  2. --request POST \

  3. --data '{"username":"xyz","password":"xyz"}' \

  4. http://localhost:3000/api/login

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值