Linux curl 用法来获取标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18820197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:51:12  来源:igfitidea点击:

curl usage to get header

linuxcurl

提问by Ankur Agarwal

Why does this not work:

为什么这不起作用:

curl -X HEAD http://www.google.com

But these both work just fine:

但是这两个都工作得很好:

curl -I http://www.google.com

curl -X GET http://www.google.com

采纳答案by dmc7z

You need to add the -i flag to the first command, to include the HTTP header in the output. This is required to print headers.

您需要将 -i 标志添加到第一个命令,以在输出中包含 HTTP 标头。这是打印标题所必需的。

curl -X HEAD -i http://www.google.com

More here: https://serverfault.com/questions/140149/difference-between-curl-i-and-curl-x-head

更多信息:https: //serverfault.com/questions/140149/difference-between-curl-i-and-curl-x-head

回答by SheetJS

google.com is not responding to HTTP HEADrequests, which is why you are seeing a hang for the first command.

google.com 未响应HTTP HEAD请求,这就是您看到第一个命令挂起的原因。

It does respond to GETrequests, which is why the third command works.

它确实响应GET请求,这就是第三个命令起作用的原因。

As for the second, curl just prints the headers from a standard request.

至于第二个,curl 只是打印标准请求的标头。

回答by bovender

curl --head https://www.example.net

curl --head https://www.example.net

I was pointed to this by curl itself; when I issued the command with -X HEAD, it printed:

我被 curl 本身指出了这一点;当我用 发出命令时-X HEAD,它打印出:

Warning: Setting custom HTTP method to HEAD with -X/--request may not work the 
Warning: way you want. Consider using -I/--head instead.