How can I get the final URL after redirects using

2019-02-11 22:45发布

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"?

5条回答
Deceive 欺骗
2楼-- · 2019-02-11 23:05

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/
查看更多
放荡不羁爱自由
3楼-- · 2019-02-11 23:11

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 
查看更多
淡お忘
4楼-- · 2019-02-11 23:15

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."

查看更多
\"骚年 ilove
5楼-- · 2019-02-11 23:22

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
查看更多
Summer. ? 凉城
6楼-- · 2019-02-11 23:23

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.

查看更多
登录 后发表回答