curl只返回http状态码的方法(windows cmd或者批处理)

怎么样用curl命令探测一个网址,只返回其HTTP状态码?一行命令就能实现:


c:\>curl -sL -w "%{http_code}" "www.baidu.com" -o /dev/null 200

-w参数的用法很重要,多放了几个例子,有助于理解-w参数的用法。


c:\>curl -sL -w "我是状态码:%{http_code}" www.baidu.com -o /dev/null 我是状态码:200

超时了。


c:\>curl -sL -w "不存在的网站?:%{http_code}" -o /dev/null www.google.com

不存在的网站?:000

404


c:\>curl -sL -w "不存在的网站?:%{http_code}" -o /dev/null http://www.guokr.com/ddddddddddddd 不存在的网站?:404

How To Display Just The HTTP Response Code In Command Line Curl

http://beerpla.net/2010/06/10/how-to-display-just-the-http-response-code-in-cli-curl/


这篇文章里列出了-w 参数的变量,用法应该一样,需要的话自行尝试。

url_effective

http_code

http_connect

time_total

time_namelookup

time_connect

time_pretransfer

time_redirect

time_starttransfer

size_download

size_upload

size_header

size_request

speed_download

speed_upload

content_type

num_connects

num_redirects

ftp_entry_path