附HTTP协议格式
请求体:请求行+请求头+空行+请求数据
-
请求行:方法字段+URL字段+HTTP协议版本,如GET/sample.jspHTTP/1.1
-
请求头:User-Agent(产生请求的浏览器类型)+Accept(客户端可识别的内容类型列表)+Host(主机地址)如
Accept:image/gif.image/jpeg,/
Accept-Language:zh-cn
Host:localhost
ser-Agent:Mozila/4.0(compatible;MSIE5.01;Window NT5.0)
Accept-Encoding:gzip,deflate -
空行:必须的,表示请求头结束
-
请求数据:username=jinqiao&password=1234
响应体:状态行+响应头+响应正文
- 状态行=HTTP-Version+Status-Code+Reason-Phrase 如HTTP/1.1 200 ok
- 响应头=服务器类型+日期+长度+内容类型等,如
Server:Apache Tomcat/5.0.12
Date:Mon,6Oct 2003 13:13:33 GMT
Content-Type:text/html
Last-Moified:Mon,6 Oct 200313:23:42 GMT
Content-Length:112
返回状态码描述见
https://www.runoob.com/http/http-status-codes.html
curl支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP等协议
一、发出 GET 请求,不带有任何参数
$ curl https://www.example.com
二、A :宣称自己的浏览器信息
指定客户端的用户代理标头,即User-Agent。curl 的默认用户代理字符串是curl/[version]。
$ curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com
移除User-Agent标头 $ curl -A ‘’ https://google.com
三、b :向服务器发送 Cookie。
生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie
$ curl -b 'foo=bar' https://google.com
$ curl -b 'foo1=bar' -b 'foo2=baz' https://google.com
读取本地文件cookies.txt,里面是服务器设置的 Cookie,将其发送到服务器
$ curl -b cookies.txt https://www.google.com
四、c:将服务器设置的 Cookie 写入一个文件
$ curl -c cookies.txt https://www.google.com
五、d:发送 POST 请求的数据体
使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST
$ curl -d'login=emma&password=123'-X POST https://google.com/login
$ curl -d 'login=emma' -d 'password=123' -X POST https://google.com/login
添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据。
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
-d参数可以读取本地文本文件的数据,向服务器发送。
$ curl -d '@data.txt' https://google.com/login
–data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。
发送的数据hello world之间有一个空格,需要进行 URL 编码
$ curl --data-urlencode 'comment=hello world' https://google.com/login
七、e:用来设置 HTTP 的标头Referer,表示请求的来源。
将Referer标头设为https://google.com?q=example
curl -e 'https://google.com?q=example' https://www.example.com
八、 F: 用来向服务器上传二进制文件
给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传
$ curl -F 'file=@photo.png' https://google.com/profile
可以指定 MIME 类型,如指定为image/png,否则 curl 会把 MIME 类型设为application/octet-stream
$ curl -F 'file=@photo.png;type=image/png' https://google.com/profile
也可以指定文件名,原始文件名为photo.png,但是服务器接收到的文件名为me.png
$ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
九、H:直接指定标头,更改User-Agent
$ curl -H 'User-Agent: php/1.0' https://google.com
-H参数可以通过直接添加标头Referer,达到同样效果。
curl -H 'Referer: https://google.com?q=example' https://www.example.com
-H参数添加 HTTP 请求的标头。
$ curl -H 'Accept-Language: en-US' https://google.com
$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
十、i:打印出服务器回应的 HTTP 标头
收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码
$ curl -i https://www.example.com
十一、I:向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。
输出服务器对 HEAD 请求的回应
$ curl -I https://www.example.com
–head参数等同于-I。
$ curl --head https://www.example.com
十二、G:用来构造 URL 的查询字符串
发出一个 GET 请求,实际请求的 URL 为https://google.com/search?q=kitties&count=20。如果省略–G,会发出一个 POST 请求。
如果数据需要 URL 编码,可以结合–data–urlencode参数
$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
$ curl -G --data-urlencode 'comment=hello world' https://www.example.com
十三、k:指定跳过 SSL 检测
不会检查服务器的 SSL 证书是否正确
$ curl -k https://www.example.com
十四、L:会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向
$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet
–limit-rate用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境
将带宽限制在每秒 200K 字节
$ curl --limit-rate 200k https://google.com
十五、o:将服务器的回应保存成文件,等同于wget命令
将www.example.com保存成example.html
$ curl -o example.html https://www.example.com
-O参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。
将服务器回应保存成文件,文件名为bar.html
$ curl -O https://www.example.com/foo/bar.html
十七、s:将不输出错误和进度信息
一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果
$ curl -s https://www.example.com
如果想让 curl 不产生任何输出,可以使用下面的命令。
$ curl -s -o /dev/null https://google.com
-S参数指定只输出错误信息,通常与-o一起使用。
下面命令没有任何输出,除非发生错误
$ curl -s -o /dev/null https://google.com
十八、u:用来设置服务器认证的用户名和密码
设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。
curl 能够识别 URL 里面的用户名和密码
$ curl -u 'bob:12345' https://google.com/login
下面命令能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头
$ curl https://bob:12345@google.com/login
只设置了用户名,执行后,curl 会提示用户输入密码
$ curl -u 'bob' https://google.com/login
十九、v:输出通信的整个过程,用于调试
$ curl -v https://www.example.com
–trace参数也可以用于调试,还会输出原始的二进制数据。
$ curl --trace - https://www.example.com
二十、x:指定 HTTP 请求的代理
指定 HTTP 请求通过myproxy.com:8080的 socks5 代理发出,如果没有指定代理协议,默认为 HTTP
$ curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com
请求的代理使用 HTTP 协议
$ curl -x james:cats@myproxy.com:8080 https://www.example.com
-X参数指定 HTTP 请求的方法。
对https://www.example.com发出 POST 请求
$ curl -X POST https://www.example.com
json格式的post请求curl -l -H “Content-type: application/json” -X POST -d ‘{“phone”:“13521389587”,“password”:“test”}’ http://domain/apis/users.json
https://www.cnblogs.com/edgedance/p/7096660.html
命令的实例使用可见链接