zoukankan      html  css  js  c++  java
  • cURL 如何只返回状态码 status code

    在写一些 Shell 测试用例时需要检测 url 的状态是否为 200,这时如果能只获取它的状态码是最理想的,cURL 可以很方便的实现。

     

    -w 可以格式化输出 reponse 的返回结果。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ curl -w "%{http_code}" https://baidu.com

    <html>
    <head><title>302 Found</title></head>
    <body bgcolor="white">
    <center><h1>302 Found</h1></center>
    <hr><center>bfe/1.0.8.18</center>
    </body>
    </html>
    302%

    访问带有跳转性质的网站,我们还需要加上 -L 做进一步跳转,同时为了避免当资源过大请求缓慢的情况,通过 -I 只返回头信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    $ curl -IL -w "%{http_code}" https://baidu.com

    HTTP/1.1 302 Moved Temporarily
    Server: bfe/1.0.8.18
    Date: Thu, 06 Jun 2019 00:25:02 GMT
    Content-Type: text/html
    Content-Length: 161
    Connection: keep-alive
    Location: http://www.baidu.com/

    HTTP/1.1 200 OK
    Accept-Ranges: bytes
    Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
    Connection: Keep-Alive
    Content-Length: 277
    Content-Type: text/html
    Date: Thu, 06 Jun 2019 00:25:05 GMT
    Etag: "575e1f60-115"
    Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
    Pragma: no-cache
    Server: bfe/1.0.8.18

    200%

    然后隐藏掉打印信息,将打印的结果输出到 /dev/null

    1
    2
    3
    4
    5
    6
    $ curl -IL -w "%{http_code}" -o /dev/null https://baidu.com
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    0 161 0 0 0 0 0 0 --:--:-- 0:00:03 --:--:-- 0
    0 277 0 0 0 0 0 0 --:--:-- 0:00:03 --:--:-- 0
    200%

    竟然还有多余信息,继续使用 -s 不显示进度和错误信息

    1
    2
    $ curl -sIL -w "%{http_code}" -o /dev/null https://baidu.com
    200%

    最后的最后,默认输出是不换行的,也就是会带有一个 % 符号,我们有两种方式去掉它

    输出换行

    1
    2
    $ curl -sIL -w "%{http_code}
    " -o /dev/null https://baidu.com
    200

    使用 echo

    1
    2
    $ echo $(curl -sIL -w "%{http_code}" -o /dev/null https://baidu.com)
    200

    -w 的一些其它参数,没有注明的可以自行测试下

    • url_effective
    • http_code 状态码
    • http_connect
    • time_total 请求总用时
    • time_namelookup DNS 域名解析的时候,就是把 https://baidu.com 转换成 ip 地址的过程
    • time_connect TCP 连接建立的时间,就是三次握手的时间
    • time_appconnect SSL/SSH 等上层协议建立连接的时间,比如 connect/handshake 的时间
    • time_redirect 从开始到最后一个请求事务的时间
    • time_pretransfer 从请求开始到响应开始传输的时间
    • time_starttransfer 从请求开始到第一个字节将要传输的时间
    • size_download
    • size_upload
    • size_header
    • size_request
    • speed_download
    • speed_upload
    • content_type
    • num_connects
    • num_redirects
    • ftp_entry_path

    https://whatua.com/2019/09/17/curl-%E6%9F%A5%E7%9C%8Bhttp%E5%90%84%E7%8E%AF%E8%8A%82%E6%97%B6%E9%97%B4/

    vim curl-time.txt 添加以下内容

    复制代码
    
             remote_ip: %{remote_ip}
    
           remote_port: %{remote_port}
    
              local_ip: %{local_ip}
    
            local_port: %{local_port}
    
                  http: %{http_code}
    
                   dns: %{time_namelookup}s
    
              redirect: %{time_redirect}s
    
          time_connect: %{time_connect}s
    
       time_appconnect: %{time_appconnect}s
    
      time_pretransfer: %{time_pretransfer}s
    
    time_starttransfer: %{time_starttransfer}s
    
         size_download: %{size_download}bytes
    
        speed_download: %{speed_download}B/s
    
                      ----------
    
            time_total: %{time_total}s
    
    
    
    复制代码

    试一试:

    复制代码
    curl  -w "@/Users/mac/Desktop/curl-time.txt" www.baidu.com
    <!DOCTYPE html>
    <!--STATUS OK--><html>*******</html>
    
             remote_ip: 180.101.49.12 #服务器ip
           remote_port: 80            #服务器端口
              local_ip: 30.208.75.45  #本地ip
            local_port: 49200         #本地端口
                  http: 200           #http状态码
                   dns: 0.030925s     #从开始到域名解析完成的时间
              redirect: 0.000000s     #所有重定向步骤(包括名称查找、连接、预传输和传输)所用的时间(秒)。显示多个重定向的完整执行时间。
          time_connect: 0.069830s     #从开始到tcp协议建立完成的时间
       time_appconnect: 0.000000s     #从开始到SSL/SSH/etc connect/handshake协议完成的时间
      time_pretransfer: 0.070846s     #从开始到文件传输即将开始
    time_starttransfer: 0.114130s     #从开始到第一个字节即将传输(time_starttransfer-time_pretransfer可以代表服务器处理的时间)
         size_download: 2381bytes     #总下载量
        speed_download: 20788.000B/s  #下载平均速度
                      ----------
            time_total: 0.114534s
    
    #也可以直接输出,不用格式化文件方式。例如:
    # curl  -w "time_total:%{time_total}" www.baidu.com
    #查看帮助信息 man curl 找到:-w, –write-out 看看哪些变量是需要的可以再添加以下。

    复制代码

    ================ End

  • 相关阅读:
    新版vscode配置eslint自动格式化代码
    vue cli4 拿到一个项目后
    windows 网页打不开github网站
    PHP+jQuery-ui拖动浮动层排序并保存到数据库实例
    php+html5兼容手机端的图片选取裁剪上传实例
    PHP+MySQL设计高效发表评论留言功能
    5种PHP生成图片验证码实例
    PHP+jPaginate插件制作无刷新分页实例
    PHP+Mysql实现网站顶和踩投票功能实例
    jQuery+PHP实现购物商城常用的星级评分效果
  • 原文地址:https://www.cnblogs.com/xzlive/p/15434561.html
Copyright © 2011-2022 走看看