如何区分 curl 最大时间和连接超时?

vgm*_*vgm 5 unix bash curl

示例命令

curl -s -w "%{http_code} %{http_connect}" --connect-timeout 10 --max-time 50
Run Code Online (Sandbox Code Playgroud)

000 000在达到连接超时和达到最大时间时返回。区分这两个错误的最佳方法是什么?

据我所知,唯一的区别是-s标志被移除的时间:

  • 连接超时返回curl: (28) connect() timed out
  • 最大超时返回 curl: (28) Operation timed out

小智 0

你的问题有两点:

  • HTTP 代码
  • 区分差异

HTTP 代码

下单失败时,不会有http code。因为没有回复。只有下单成功后才能获取http code。

区分差异

您可以通过以下顺序获得错误。

result=`curl  --connect-timeout $connectiontimeout --max-time $maxtimeout -s -S -X POST -H 'Content-Type: text/plain' -d "$DATA" "$resturl" 1>&1 2>&1`

if [ "$result" = "curl: (28) connect() timed out" ] ;then
    echo "curl: (28) connect() timed out"

fi
Run Code Online (Sandbox Code Playgroud)

然后就可以通过判断结果来区分这两个错误。