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

前端 未结 5 1801
执念已碎
执念已碎 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
    

提交回复
热议问题