的Net :: HTTP不提供以下的重定向的一個很好的方式,這裏是一段代碼,我一直在使用,現在有一段:
require 'net/http'
class RedirectFollower
class TooManyRedirects < StandardError; end
attr_accessor :url, :body, :redirect_limit, :response
def initialize(url, limit=5)
@url, @redirect_limit = url, limit
end
def resolve
raise TooManyRedirects if redirect_limit < 0
self.response = Net::HTTP.get_response(URI.parse(url))
if response.kind_of?(Net::HTTPRedirection)
self.url = redirect_url
self.redirect_limit -= 1
resolve
end
self.body = response.body
self
end
def redirect_url
if response['location'].nil?
response.body.match(/<a href=\"([^>]+)\">/i)[1]
else
response['location']
end
end
end
wordpress = RedirectFollower.new('http://wordpress.org/latest.zip').resolve
puts wordpress.url
File.open("latest.zip", "w") do |file|
file.write wordpress.body
end
謝謝!一直在掙扎。 Net :: HTTP確實不INDEED NOT很好地處理重定向。謝謝你的片段! – maetthew 2011-03-22 03:06:03
你讓我好開心。謝謝! – Stone 2012-11-10 08:28:47