curl工具的使用

curl工具的使用

curl是常用的命令行工具,用来请求Web服务器。curl的就是client url的意思。

不带任何参数时,curl发出的是GET请求

curl https://www.example.com

-A

-A参数指定客户端的用户代理标头,即User-Agent。curl的默认用户代理字符串是curl/[version]

# 将User-Agent改成Chrome浏览器
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

# 通过-H直接指定标头
curl -H 'User-Agent:python/3.6'

-b

-b参数用来向服务器发送Cookie

curl -b 'username=qq' https://qq.com

上述命令会生成一个标头Cookie:username=qq,向服务器发送一个名为username,值为qq的Cookie

curl -b 'username=qq;qqzone=1' https://qq.com

上面命令发送两个Cookie

curl -b cookies.txt https://qq.com

上面的命令读取本地文件cookies.txt,里面是服务器设置的Cookie,将其发送到服务器


-c

-c参数将服务器设置的Cookie写入一个文件

curl -c cookies.txt https://www.google.com

上述命令将服务器的HTTP响应所设置Cookie写入本地文本文件cookies.txt


-d

-d参数用于发送POST请求的数据体

curl -d 'login=emmmm&password=23333' -X POST https://google.com/login
# 或者
curl -d 'login=emmmm' -d 'password=23333' -X POST https://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

--data-urlencode参数等同于-d,发送POST请求的数据体,区别在于会自动将发送的数据进行URL编码

curl --data-urlencode 'comment=hello world' https://google.com/login

上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码


-e

curl -e 'https://google.com?q=example' https://example.com

上面把Referer标头设为https://google.com?q=example

-H参数可以通过直接添加标头Referer,达到同样的效果

curl -H 'Referer:https://google.com?q=example' https://www.example.com

-F

-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

-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标头Accept-Language:en-US

curl -H 'Accept-Language:en-US' -H 'Secret-Message:xyzzy' https://google.com

上面添加两个HTTP标头

curl -d '{"login": "emmm", "pass": "2333"}' -H 'Content-Type:application/json'

上面命令添加了一个HTTP请求标头Content-Type:application/json,然后用-d参数发送json数据


-i

-i参数打印出服务器响应的HTTP标头

curl -i https://www.example.com

上面命令收到服务器响应后,先输出服务器响应头,然后空一行,再网页的源码输出


-I

-I参数向服务器发出HEAD请求,然后将服务器返回的HTTP标头打印出来

curl -I https://www.example.com

上面命令输出对HEAD请求的响应

--head参数等同于-I

curl --head https://www.example.com

-k

参数指定跳过SSL检测

curl -k https://www.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

-o参数将服务器的响应保存成文件,等同于wget命令

curl -o example.html https://www.example.com

上面命令将www.example.com保存成example.html


-O

-O参数将服务器响应保存成文件,并将URL的最后部分当作文件名

curl -O https://www.example.com/foo/bar.html

上面命令将服务器响应保存成文件,文件名bar.html


-S

-s参数将不输出错误和进度信息

curl -s https://www.example.com

上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果

如果想让curl不产生任何输出,可以使用下面的命令

curl -s -o /dev/null https://google.com

上面命令没有任何输出,除非发生错误


-u

-u参数用来设置服务器认证的用户名和密码

curl -u 'bob:123' https://google.com/login

上面命令设置用户名bob,密码为12345,燃火将其转为HTTP标头,然后将其转为HTTP标头Authorization: Basic Ym9iOjEyMzQ1

curl能够识别URL里面的用户名和密码

curl https://bob:123@google.com/login

上面命令能够识别URL里面的用户名和密码,将其转为上个例子里面的HTTP标头

curl -u 'bob' https://google.com/login

上面命令只设置了用户名,执行后,curl 会提示用户输入密码


-v

-v参数输出通信的整个过程,用于调试

curl -v https://www.example.com

-x

-x参数指定HTTP请求的代理

curl -x socks5://james:cat@myproxy.com:8080 https://www.example.com

上面命令指定HTTP请求通过myproxy.com:8080的socks5代理发出

如果没有指定代理协议,默认为HTTP

curl -x james:cat@myproxy.com:8080 https://www.example.com

上面命令中,请求的代理使用HTTP协议


-X

-X参数指定HTTP请求的方法

curl -X POST https://www.example.com

上面命令对https://www.example.com发出POST请求


补充:

1)监控网页响应时间

curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "https://www.zongze.net"

其中

time_connect 建立到服务器的 TCP 连接所用的时间

time_starttransfer 在发出请求之后,Web 服务器返回数据的第一个字节所用的时间

time_total 完成请求所用的时间

-s 静默输出

posted @ 2020-03-25 22:01  乘月归  阅读(1115)  评论(0编辑  收藏  举报