且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Ruby重定向后如何获得最终的URL?

更新时间:2023-11-26 16:41:28

有两种方法,同时使用 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/