2013-05-01 71 views
2

我是一種新的Ruby和python背景 我想對URL做一個頭部請求並檢查一些信息,比如服務器上是否存在文件以及時間戳,etag等,我無法在RUBY中完成這項工作。在Ruby中製作HEAD請求

在Python:

import httplib2 
print httplib2.Http().request('url.com/file.xml','HEAD') 

在Ruby:我嘗試這樣做,拋出一些錯誤

require 'net/http' 

Net::HTTP.start('url.com'){|http| 
    response = http.head('/file.xml') 
} 
puts response 


SocketError: getaddrinfo: nodename nor servname provided, or not known 
    from /Users/comcast/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:877:in `initialize' 
    from /Users/comcast/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:877:in `open' 
    from /Users/comcast/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:877:in `block in connect' 
    from /Users/comcast/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/timeout.rb:51:in `timeout' 
    from /Users/comcast/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:876:in `connect' 
    from /Users/comcast/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:861:in `do_start' 
    from /Users/comcast/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:850:in `start' 
    from /Users/comcast/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:582:in `start' 
    from (irb):2 
    from /Users/comcast/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>' 
+0

什麼是你'url'哈希? – 2013-05-01 20:28:43

回答

7

我不認爲這傳遞一個字符串:開始是不夠的; in the docs它看起來像它需要一個URI對象的一個​​正確地址的主機和端口:

uri = URI('http://example.com/some_path?query=string') 

Net::HTTP.start(uri.host, uri.port) do |http| 
    request = Net::HTTP::Get.new uri 

    response = http.request request # Net::HTTPResponse object 
end 

你可以試試這個:

require 'net/http' 

url = URI('yoururl.com') 

Net::HTTP.start(url.host, url.port){|http| 
    response = http.head('/file.xml') 
    puts response 
} 

一件事我注意到 - 你puts response需求是塊裏面!否則,變量response不在範圍內。

編輯:您還可以治療的反應作爲哈希以獲得標題的值:

response.each_value { |value| puts value } 
+0

謝謝。 priti,我正在嘗試的內部URL你不能訪問它。但一般來說,它是一個下載xml文件的網址。我不想在我知道它之前下載它,就像它是陳舊的,重複等等,所以頭請求不下載它,但取而代之的是取回屬性 – Krish 2013-05-01 21:05:28

+0

我試過你的第二種方法,但它只是返回這個值「## 「,我期待着很多關於文件的頭文件信息和屬性 – Krish 2013-05-01 21:06:19

+0

我期待着這樣的信息({'status':'200','content-length':'2983', 'accept-ranges':'bytes','server':'Apache/2.2.17(Unix)','last-modified':'Wed,01 May 2013 20:53:26 GMT','etag':' 「5f56a-ba7-4dbae4f35555」','date':'Wed,2013年5月1日21:11:30 GMT','content-type':'application/xml'},'') – Krish 2013-05-01 21:11:59

3

我知道這已經回答了,但我不得不去通過一些籃球了。這裏有更具體的東西入手:

#!/usr/bin/env ruby 

require 'net/http' 
require 'net/https' # for openssl 

uri = URI('http://stackoverflow.com') 
path = '/questions/16325918/making-head-request-in-ruby' 

response=nil 
http = Net::HTTP.new(uri.host, uri.port) 
# http.use_ssl = true       # if using SSL 
# http.verify_mode = OpenSSL::SSL::VERIFY_NONE # for example, when using self-signed certs 

response = http.head(path) 
response.each { |key, value| puts key.ljust(40) + " : " + value } 
2
headers = nil 

url = URI('http://my-bucket.amazonaws.com/filename.mp4') 

Net::HTTP.start(url.host, url.port) do |http| 
    headers = http.head(url.path).to_hash 
end 

現在你有標題的headers