CURL的学习
基础参数
curl 网址:
curl www.sina.com
curl -o [文件名] www.sina.com
3. 自动跳转
-L
:curl跳转到新的网址
curl -L www.sina.com === curl www.sina.com.cn
4. 现实头信息
-i
:显示http response
中的头信息以及网站源码
-I
:只显示头信息
5. 显示通信过程
-v
:显示一次http
通信过程,包括端口连接和http request头信息
查看更详细的通信过程
curl --trace output.txt www.sina.com
或者
curl --trace-ascii output.txt www.sina.com
运行后,打开output.txt
文件
6. 发送表单信息
- get方式
curl example.com/form.cgi?data=xxx
- post方式(–data参数)
需要指明方式,并且将数据和网址分开
curl -X POST --data "data=xxx" example.com/form.cgi
POST还可对数据进行编码
curl -X POST --data-urlencode "data=xxx" example.com/form.cgi
7. http动词
-X
:指定curl
的http
请求方式,默认是GET
方式
curl -X POST www.com/form.cgi
curl -X DELETE www.com/fomr.cgi
8. 文件上传
文件上传的表单
<form method="POST" enctype='multipart/form-data' action="upload.cgi">
<input type=file name=upload>
<input type=submit name=press value="OK">
</form>
--form
:上传文件
curl --form upload=@localfilename --form press=OK [URL]
9. Referer字段
--referer
:在http request
请求头中提供referer
字段
curl --referer http://www.example.com http://www.example.com
10. User Agent字段
User Agent
:用于识别设备信息
Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
curl --user-agent "[User Agent]" [URL]
11. cookie
--cookie
:利用curl
发送cookie
curl --cookie "name=xxx" www.example.com
12. 添加头信息
--header
:添加头信息
curl --header="Content-Type:application/json" http://example.com
13. HTTP认证
--user
:进行http
认证
curl --user name:password example.com
用法指南
-A(User-Agent)
-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
也可以移除标头
$ curl -A '' https://google.com
-b(发送cookie)
向服务器发送cookie
参数
curl -b 'foo:bar' http://example.com
也可以读取本地文件当作cookie
curl -b cookie.txt http://example.com
-c(保存cookie)
将服务器的cookie
保存到文件
curl -c cookie.txt http://example.com
-d(POST数据)
发送POST
请求的数据体
curl -d 'login=emma&password=123' -X POST http://google.com/login
使用-d
参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded
。并且会自动将请求转为 POST 方法,因此可以省略-X POST
。
-d
参数可以读取本地文本文件的数据,向服务器发送。
curl -d '@data.txt' https://google.com/login
上面命令读取data.txt
文件的内容,作为数据体向服务器发送。
–data-urlencode(POST编码)
对POST数据进行编码
curl --data-urlencode 'comment=hello world' https://google.com/login
上面代码中,发送的数据hello world
之间有一个空格,需要进行 URL 编码。
-e(referer)
设置referer
头
curl -e 'https://google.com?q=example' http://example.com
-F(文件)
传输二进制文件
curl -F 'file=@photo.png' https://google.com/profile
上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data
,然后将文件photo.png
作为file
字段上传。
-F
参数可以指定 MIME 类型。
curl -F 'file=@photo.png;type=image/png' https://google.com/profile
上面命令指定 MIME 类型为image/png
,否则 curl 会把 MIME 类型设为application/octet-stream
。
-F
参数也可以指定文件名。
curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
上面命令中,原始文件名为photo.png
,但是服务器接收到的文件名为me.png
。
-G(查询字符串)
-G
构造url
查询字符串
curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
上面命令会发出一个 GET 请求,实际请求的 URL 为https://google.com/search?q=kitties&count=20
。如果省略--G
,会发出一个 POST 请求。
如果数据需要 URL 编码,可以结合--data--urlencode
参数。
curl -G --data-urlencode 'comment=hello world' https://www.example.com
-H(设置请求标头)
-H
参数添加 HTTP 请求的标头。
curl -H 'Accept-Language: en-US' https://google.com
添加 HTTP 请求的标头是Content-Type: application/json
,然后用-d
参数发送 JSON 数据。
curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
-i/I(打印标头)
打印http response
返回的标头以及源码/标头
--head=-I
curl -I http://example.com
-K(跳过证书检测)
-k
参数指定跳过 SSL 检测。
curl -K http://example.com
上面命令不会检查服务器的SSL
证书是否正确。
-L(重定向)
-L
参数会让 HTTP 请求跟随服务器的重定向。curl
跟随重定向
curl -L -d 'tweet=hi' https://api.twitter.com/tweet
–limit-rate(限制带宽)
--limit-rate
用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。
curl --limit-rate 200k https://google.com
上面命令将带宽限制在每秒 200K 字节。
-o(response保存为文件)
将http response
参数保存成文件,相当于wget
curl -o example.html https://www.example.com
-O(response保存为文件)
将http response
参数保存成文件,并用url
最后部分当作文件名
curl -O https://www.example.com/foo/bar.html
上面命令将服务器回应保存成文件,文件名为bar.html
。
-s(不输出错误信息)
curl
不输出任何错误信息
curl -s https://www.example.com
如果想让 curl 不产生任何输出,可以使用下面的命令。
curl -s -o /dev/null https://google.com
-S(输出错误信息)
-S
参数指定只输出错误信息,通常与-s
一起使用。
curl -S -o /dev/null https://google.com
上面的命令不会输出信息,除非报错
-u(设置服务器认证)
-u
参数用来设置服务器认证的用户名和密码。
curl -u 'boo:1234' https://google.com/login
设置用户名为boo
密码为1234
,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1
。
curl
能够识别URL
里面的用户名和密码。
curl https://bob:12345@google.com/login
-v(输出通信过程)
-v
参数,输出通信的整个过程,进行调试
curl -v https://www.example.com
--trace
参数也可以用于调试,还会输出原始的二进制数据。
-x(指定代理)
-x
参数指定HTTP
请求的代理。
curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com
上面命令指定 HTTP 请求通过myproxy.com:8080
的 socks5 代理发出。
如果没有指定代理协议,默认为 HTTP。
-X(指定请求方式)
curl -X POST https://www.example.com
对https://www.example.com
发出 POST 请求。