Dave Thomas <Dave / thomases.com> writes: > Minero Aoki <aamine / dp.u-netsurf.ne.jp> writes: > > > Hi, > > > > In mail "[ruby-talk:03161] Re: Retrieving the hostname and port in net/http" > > Dave Thomas <Dave / thomases.com> wrote: > > > > > One of the problems is that HTTP#get throws an exception on a 301, so > > > there's no resp structure to interrogate. > > > > Yes, that's right. I agree that "get" method is OUT OF USE for > > many applications. I want to make "get" not to raise exceptions, > > but I could not because of backward compatibility. > > If it is not too rate, I'll change it. > > Either that, or possibly include the header information in the > exception. And, just to show how responsive the Ruby developers are, less than 24 hours later, Minero Aoki checks in to the CVS tree a new net/http that does just that (and in doing so gives us a reason to use the 'retry' feature of Ruby exceptions): require 'net/http' h = Net::HTTP.new(ARGV[0] || 'www.ruby-lang.org', 80) url = ARGV[1] || '/' begin puts "Fetching #{url}" resp, data = h.get(url, nil) puts data rescue Net::ProtoRetriableError => detail head = detail.data if head.code == "301" puts "Need to retry" url = head['location'] url =~ %r{^http://([^/]+)(.*)} host = $1 url = $2 port = 80 if host =~ /(.*):(\d+)/ host = $1 post = $2 end h.finish h = Net::HTTP.new(host, port) retry # Back, Jack, and do it again! end end Regards Dave