如何在使用Ruby重定向后获取最终的URL?

Mar*_*ark 17 ruby networking redirect url-routing

如果http://foo.com重定向到重定向到的1.2.3.4重定向http://finalurl.com,我如何使用Ruby找到登陆URL"http://finalurl.com"?

the*_*Man 23

这里有两种方法,使用HTTPClientOpen-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/
Run Code Online (Sandbox Code Playgroud)