一、curl的常见使用场景
1、查看网页源码
[root@localhost ~]# curl www.baidu.com
<!DOCTYPE html>
<html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><
meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn
2、查看头信息
[root@localhost ~]# curl -i www.baidu.com
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Mon, 12 Mar 2018 16:02:33 GMT
Content-Type: text/html
Content-Length: 2381
Last-Modified: Mon, 23 Jan 2017 13:28:12 GMT
Connection: Keep-Alive
ETag: "588604ec-94d"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Set-Cookie: BDORZ=27315
Accept-Ranges: bytes
3、显示请求详细信息
curl -v www.baidu.com
4. 指定请求方式
Get请求
curl -X GET http:
POST请求
curl -X POST -d"data=123&key=456" http:
使用JSON形式的POST数据
curl -H "Content-Type:application/json" -d '{"data":"123","key":"456"}' http:
请求时带上Cookie
curl -H "Cookie:username=XXX" {URL}
DELETE请求:DELETE请求用于删除服务器端的数据。
curl -X DELETE http:
PUT请求:PUT请求用于修改服务器端的数据
curl -X PUT http:
5、Cookie相关
-c存储到cookie到文件
curl -d"name=renwoxing&password=123" http:
-b携带cookie文件
curl --cookie "name=zhangsan" http:
--cookie 直接指定cookie文件
curl --cookie "name=renwoxing" http:
-F表单提交操作
curl -F prefile=@portrait.jpg https:
向服务器上传一个图片,图片的表单域名为profile,内容为protrait.jpg的二进制
6、HTTP认证
常见HTTP认证方式:Basic认证、Digest认证、OAuth2认证。
Basic认证
curl --basic -u user:password http:
Digest认证
curl --digest -u user:password http:
OAuth2认证
curl -u clientId:clientSecret -X POST -d "username=test&password=test&grant_type=password&scope=read" http:
curl -H "Authorization: Bearer [bearer]" http:
7、下载网页
curl -o file.html http://www.example.com
重命名下载的文件
curl -O http://www.example.com/1.jpg
8、代理服务器
curl -D cookiefile01.txt http://www.example.com