开发者

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://finalur开发者_如何学Cl.com, how can I use Ruby to find out the landing URL "http://finalurl.com"?


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/


Another way, using 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 


for JRuby this worked

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


I have implemented a RequestResolver for my need:

https://gist.github.com/lulalala/6be104641bcb60f9d0e8

It uses Net::HTTP, and follows multiple redirects. It also handles relative redirects. It was for my simple need so may have bugs. If you discover one please tell me.


I'm not much of a Ruby user, but what you basically need is something to interpret HTTP headers. The following library appears to do that:

http://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html

Skip down to "following redirection."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜