返回总目录页

curl命令详解

 

 https://blog.csdn.net/jackyzhousales/article/details/82799494

curl请求https需要添加参数-k

请求查看响应头信息可以用-I

[machangwei@localhost ~]$ curl -I https://es.b2b.10086.cn/
curl: (60) Peer's Certificate issuer is not recognized.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
[machangwei@localhost ~]$ curl -I -k https://es.b2b.10086.cn/
HTTP/1.1 200 OK
Date: Sat, 20 Nov 2021 01:53:50 GMT
Server: Apache
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Type: text/html
Content-Length: 45
Connection: Keep-alive
Via: 1.1 ID-0002262061156206 uproxy-17

 

curl -L 访问永久跳转的页面

[root@host1 ~]$ curl -L -I 10.0.0.134:8500
HTTP/1.1 301 Moved Permanently
Location: /ui/
Date: Sun, 02 Jan 2022 06:33:40 GMT
Content-Type: text/plain; charset=utf-8

HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 28045
Content-Type: text/html; charset=utf-8
Last-Modified: Tue, 30 Jun 2015 19:59:35 GMT
Date: Sun, 02 Jan 2022 06:33:40 GMT

 

 

1. -X 指定请求方式
GET请求
curl -X GET http://www.jackyops.com/search?data=123 # -X GET是可选的

POST请求
curl -X POST -d"data=123&key=456" http://www.jackyops.com/search -v
由于-d选项为使用POST方式向server发送数据,因此在使用-d的时候,可以省略-X POST。使用-d时,将使用Content-type:application/x-www-form-urlencoded方式发送数据。
如果想使用JSON形式post数据,可以使用-H指定头部类型
curl -H "Content-Type:application/json" -d '{"data":"123","key":"456"}' http://www.jackyops.com/search -v

如果想在请求的时候带上Cookie,可以这样
curl -H "Cookie:username=XXX" {URL}

2、开启gzip请求
curl -I http://www.baidu.com/ -H Accept-Encoding:gzip,defalte

3、监控网页的响应时间
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.htcview.com"
time_connect: 0.015
time_starttransfer: 0.197
time_total: 0.245

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

4. 监控站点可用性
curl -o /dev/null -s -w %{http_code} "http://www.htcview.com"

5、以http1.0协议请求(默认为http1.1)
curl -o ..............

监控站点首页下载时间:
curl -o /dev/null -s -w ‘%{time_total}’ http://www.htcview.com
curl -o /dev/null -s -w ‘%{http_code}’ http://www.htcview.com
curl -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total} http://www.htcview.com


-s 静默输出;没有-s的话就是下面的情况,这是在脚本等情况下不需要的信息。

[jacky@jackyops ~]$ curl -o /dev/null -w ‘%{time_total}’ http://www.htcview.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 58276 0 58276 0 0 10130 0 --:--:-- 0:00:05 --:--:-- 13741
结果:‘5.753’
监控首页各项时间指标:

curl -o /dev/null -s -w ‘%{time_connect}:%{time_starttransfer}:%{time_total}’ http://www.htcview.com
‘0.519:0.703:0.752’


时间指标解释 :
time_connect 建立到服务器的 TCP 连接所用的时间
time_starttransfer 在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
time_total 完成请求所用的时间

在 发出请求之后,Web 服务器处理请求并开始发回数据所用的时间是
(time_starttransfer)1.044 - (time_connect)0.244 = 0.8 秒

客户机从服务器下载数据所用的时间是
(time_total)2.672 - (time_starttransfer)1.044 = 1.682 秒

指定特定主机IP地址访问网站
curl -x 106.11.208.145:80 http://www.youku.com


6. curl用法大全
-x 指定访问IP与端口号
curl -x 192.168.4.12:80 http://www.jackyops.com

-I 仅仅取文件的http头部
curl -I -x 192.168.4.12:80 http://www.jackyops.com

用referer做的防盗链,就可以使用-e来设置
curl -e “http://www.images.org” http:// www.jackyops.com -v -I

-H去构造你想要的http头部
curl -H “X-Forward-For:8.8.8.8″ http://www.jackyops.com -v -I

curl反馈时间,例如连接时间,下载时间等信息
curl -w %{time_connect}:%{time_starttransfer}:%{time_total} -s -o /dev/null

将一个文件保存到硬盘上,命名为file.html
curl -o file.html http://www.jackyops.com/index.html

下载index.html文件, -O是大写的字母
curl -O http://www.jackyops.com/index.html

curl提交用户名和密码
curl http://name:passwd@www.jackyops.com
curl -u name:passwd http://www.jackyops.com

-b "cookie" 此参数用来构造一个携带cookie的请求
前面讲到了使用 -H 来发送 Cookie 的方法,这种方式是直接将 Cookie 字符串写在命令中。如果使用 -b 来自定义 Cookie,命令如下:
curl -b “JSESSIONID=D0112A5063D938586B659EF8F939BE24” http://www.jackyops.com
如果要从文件中读取 Cookie,-H 就无能为力了,此时可以使用 -b 来达到这一目的:
curl -b “cookie-example” http://www.jackyops.com
即 -b 后面既可以是 Cookie 字符串,也可以是保存了 Cookie 的文件名。

 

# curl -so /dev/null -w "%{http_code}" https://127.0.0.1:8200/v1/sys/health -k
503#

 

posted @ 2021-11-21 10:30  马昌伟  阅读(3115)  评论(0编辑  收藏  举报
博主链接地址:https://www.cnblogs.com/machangwei-8/