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 的文件名。