How can I get the final URL after redirects using Ruby?

前端 未结 5 1802
执念已碎
执念已碎 2020-12-17 10:35

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 \"h

相关标签:
5条回答
  • 2020-12-17 11:09

    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
    
    0 讨论(0)
  • 2020-12-17 11:12

    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.

    0 讨论(0)
  • 2020-12-17 11:15

    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 
    
    0 讨论(0)
  • 2020-12-17 11:25

    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 讨论(0)
  • 2020-12-17 11:34

    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/
    
    0 讨论(0)
提交回复
热议问题