作用
作为客户端发送请求
HTTP常见请求
curl URL【-X -GET: 默认是 get请求】
curl -X -POST URL 【设定为post请求】或-XPOST
curl -X -POST http://172.x.x.x:8000/curl_test
curl -XPOST URL -d 数据 【在 -d 之后跟着要添加的数据】
curl -XPOST http://172.x.x.x:8000/curl_test -d '{"msgId":"035e-a601","sendType":1,"toUsers":"+8618888888888","type":1}'
HTTP首部
curl -URL -H 首部 【-H, 把首部添加到请求里, 在-H后添加首部】可以添加多个 -H 首部
curl -XPOST http://172.x.x.x:8000/curl_test -H "Content-Type:application/json" -d '{"msgId":"035e-a601","sendType":1,"toUsers":"+8618888888888","type":1}'
curl -I URL 【-I, 获取到响应里的所有首部】
curl -XPOST -I http://172.x.x.x:8000/curl_test -H "Content-Type:application/json" -d '{"msgId":"035e-a601","sendType":1,"toUsers":"+8618888888888","type":1}'
文件处理
curl -O URL 【-O, 把资源文件下载到当前所处的文件夹里】
curl -XPOST -O -I http://172.x.x.x:8000/curl_test/download/hello.jpg -H "Content-Type:application/json" -d 数据
curl -o hi.jpg URL 【-o 新文件名, 重定义文件名】
curl -XPOST -o hi.jpg -I http://172.x.x.x:8000/curl_test/download/hello.jpg -H "Content-Type:application/json" -d 数据
curl --limit-rate 速度 【–limit-rate 速度, 限制下载速度】
curl -XPOST -o hi.jpg --limit-rate 100k -I http://172.x.x.x:8000/curl_test/download/hello.jpg -H "Content-Type:application/json" -d 数据
curl -C - URL 【-C -, 如果终止 恢复下载】
curl -XPOST -o hi.jpg -C - -I http://172.x.x.x:8000/curl_test/download/hello.jpg -H "Content-Type:application/json" -d 数据
连接与测试
curl -L URL 【-L, 跟随请求重定向】
curl -XPOST -O -I http://172.x.x.x:8000/curl_test/download/hello.jpg -L -H "Content-Type:application/json" -d 数据
curl -v URL 【-v, 会把底层的连接信息都显示出来】
curl -XPOST -O -I http://172.x.x.x:8000/curl_test/download/hello.jpg -v -L -H "Content-Type:application/json" -d 数据
curl --proxy “http://用户名:密码@代理地址:端口” URL 【通过代理访问】
curl -u 用户名:密码 -O ftp://server/hello.png 【-u: 使用小写u认证用户名密码, 使用-O或-o下载文件】
curl -u 用户名:密码 -T ftp://server 【-T: 上传文件 注意目录】
实际应用
使用【实体】接收参数
curl -H "Content-Type:application/json" -X POST -d '{"msgId":"035e-a601","sendType":1,"toUsers":"+8619180531410","type":1}' 'http://172.x.x.x:8000/curl_test'
使用【@RequestParam】接收参数,@RequestParam(“filePath”) String filePath, @RequestParam(“type”) String type
curl -XPOST http://172.x.x.x:8000/curl_test -d"filePath=/data/channel/media/20080913/01/&type=涉政"