递归 curl -l 获取网页的最终状态

递归 curl -l 获取网页的最终状态

我希望知道某个网页是否返回 200/OK HTTP 状态代码 (HSC)。当然,我可以第一的通过执行 来对网页进行 HSC curl -l URL

问题是,许多网页都有重定向,并且curl不会自然地顺着这些重定向流动,所以它会在第一个 HSC 中停止,而不会继续最后的HSC 和回报:

301:永久移动

这并不能帮助我了解,如果最后的用户将被移动到的网页是好是坏,或者只是它是否返回 200。

我如何确保curl继续执行 URL 和重定向(如果有)直到到达最后一个 URL 以检查它是否返回 200?

目的是要知道用户最终是否应该到达典型网页(200/OK)。

答案1

要遵循重定向,请使用以下-L标志:

curl -L mywebpage.com

curl 的手册页:

-L, --location
              (HTTP/HTTPS) If the server reports that the requested page has moved to a different location (indicated  with  a  Location:  header  and  a  3XX
              response  code),  this  option will make curl redo the request on the new place. If used together with -i, --include or -I, --head, headers from
              all requested pages will be shown. When authentication is used, curl only sends its credentials to the initial host. If a redirect takes curl to
              a  different  host, it won't be able to intercept the user+password. See also --location-trusted on how to change this. You can limit the amount
              of redirects to follow by using the --max-redirs option.

              When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a  GET  if  the
              HTTP  response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmodi‐
              fied method.

              You can tell curl to not change the non-GET request method to GET after a 30x response by using  the  dedicated  options  for  that:  --post301,
              --post302 and -post303.

相关内容