的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