1. 程式人生 > 實用技巧 >linux-curl工具使用

linux-curl工具使用

環境:centos

1、超時引數解析

#使用curl時,有兩個超時時間:
#--connect-timeout <seconds>     是連線超時時間
#-m/--max-time     <seconds>     是整個過程允許的最大時間 
#下面表示連線百度20秒沒有連線到百度退出,整個過程40s沒有返回退出
curl --connect-timeout 20 -m 40 https://www.baidu.com

2、靜默不輸出資訊 -s引數

[root@minion246 wj]# curl http://gayd.sczwfw.gov.cn:8903|grep mess                     #不帶-s
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    62  100    62    0     0   1444      0 --:--:-- --:--:-- --:--:--  1476
{"code":40966,"message":"請求的應用token位數不正確"}
[root@minion246 wj]# curl -s http://gayd.sczwfw.gov.cn:8903|grep mess                 #帶引數-s
{"code":40966,"message":"請求的應用token位數不正確"}

3、獲取對方的響應首部資訊 -I

[root@minion246 wj]# curl -I http://gayd.sczwfw.gov.cn
HTTP/1.1 200 OK
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,HEAD,PUT,POST,DELETE
Content-Type: text/plain; charset=utf-8
Content-Length: 0
Date: Wed, 23 Sep 2020 03:16:25 GMT
Connection: keep-alive

4、顯示網頁的全部資訊

curl -i  "https://www.baidu.com" 

5、顯示一次的http請求的通訊過程

[root@minion246 wj]# curl -v http://gayd.sczwfw.gov.cn
* About to connect() to gayd.sczwfw.gov.cn port 80 (#0)
*   Trying 202.61.88.250...
* Connected to gayd.sczwfw.gov.cn (202.61.88.250) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: gayd.sczwfw.gov.cn
> Accept: */*

6、模擬get 請求

當發起http請求時,curl會預設發起GET請求,也可以"-X GET"方式指定
curl -X GET http://www.baidu.com

7、模擬post提交

1、有body引數
curl -d "param1=value1&param2=value2" "http://www.baidu.com"
2、有JOSN格式的body和header入參
curl -s -l -H "Content-type: application/json" -X POST -d @/wj/smrz.xml https://gayd.sczwfw.gov.cn/e62894182afb55c9a65baab9ba9b6d18