2013-05-05 160 views
1

,卷曲我可以像这样进行HTTP头请求:请与路边一个HTTP头请求

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

如何我路沿石执行此过程?我不想检索身体,因为这会花费太多时间。

回答

4

-I/--head选项执行HEAD请求。使用libcurl C API,您需要设置CURLOPT_NOBODY选项。

h = Curl::Easy.new("http://www.google.com") 
h.set :nobody, true 
h.perform 
puts h.header_str 
# HTTP/1.1 302 Found 
# Location: http://www.google.fr/ 
# Cache-Control: private 
# Content-Type: text/html; charset=UTF-8 
# ... 

作为替代方案,您可以像使用便利的捷径之一:

h = Curl::Easy.new("http://www.google.com") 
# This sets the option behind the scenes, and call `perform` 
h.http_head 
puts h.header_str 
# ... 

或者这样一个

随着路边,你可以根据以下步骤在您的手柄设置此选项,使用类方法:

h = Curl::Easy.http_head("http://www.google.com") 
puts h.header_str 
# ... 

注意:最终的快捷方式是Curl.head("http://www.google.com")。这是说等到下一个路边释放使用它之前,因为它是不是工作在写这篇文章,并刚刚被修补:请参阅此pull request