How can I get the final URL after redirects using Ruby?
问题 If http://foo.com redirects to 1.2.3.4 which then redirects to http://finalurl.com , how can I use Ruby to find out the landing URL "http://finalurl.com"? 回答1: Here's two ways, using both HTTPClient and 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/ 回答2: