使用Ruby重定向后如何获得最终的URL? [英] How can I get the final URL after redirects using Ruby?
本文介绍了使用Ruby重定向后如何获得最终的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果http://foo.com
重定向到1.2.3.4
,然后又重定向到http://finalurl.com
,我如何使用Ruby找出登陆URL"http://finalurl.com"?
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"?
推荐答案
有两种方法,同时使用 HTTPClient 和 Open-URI :
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/
这篇关于使用Ruby重定向后如何获得最终的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文