如果Ruby重定向到1.2.3.4
,然后重定向到http://finalurl.com
,我如何使用http://foo.com
找到登录URL "http://finalurl.com"?
发布于 2011-02-02 05:03:59
这里有两种同时使用HTTPClient和Open-URI的方法
require 'httpclient'
require 'open-uri'
URL = 'http://www.example.org'
httpc = HTTPClient.new
resp = httpc.get(URL)
puts resp.header['Location']
>> http://www.iana.org/domains/example/
open(URL) do |resp|
puts resp.base_uri.to_s
end
>> http://www.iana.org/domains/example/
发布于 2012-09-15 16:57:24
另一种方式,使用Curb
def get_redirected_url(your_url)
result = Curl::Easy.perform(your_url) do |curl|
curl.follow_location = true
end
result.last_effective_url
end
发布于 2015-03-24 13:01:02
对于JRuby
来说,这是可行的
def get_final_url (url)
final_url = ""
until url.nil? do
final_url = url
url = Net::HTTP.get_response(URI.parse(url))['location']
end
final_url
end
https://stackoverflow.com/questions/4867652
复制相似问题