302

Hi,
i’m usin ver 1.6.7 of ruby . Everytime I try to access a site via Net::HTTP
and this site has a 302 response code it says

/net/http.rb:1432: in error! : 302 “Found” (Net::ProtoRetriableError)

Ruby terminates the script with this errormessage. But I want to read out
the new location of the url. Does anybody know how to get this url or if
there is any possibility to solve this problem?

Hi,
i’m usin ver 1.6.7 of ruby . Everytime I try to access a site via Net::HTTP
and this site has a 302 response code it says

/net/http.rb:1432: in error! : 302 “Found” (Net::ProtoRetriableError)

Ruby terminates the script with this errormessage. But I want to read out
the new location of the url. Does anybody know how to get this url or if
there is any possibility to solve this problem?

I don’t know about reading the “new” URL, but to prevent the program from
terminating, you should rescue Net::ProtoRetriableError. There’s probably a
superclass you can rescue as well. Let me see…

irb(main):001:0> require ‘net/http’
true
irb(main):003:0> Net::ProtoRetriableError.ancestors
[Net::ProtoRetriableError, Net::ProtocolError, StandardError, Exception,
Object, Kernel]

Yes, I’d rescue Net::ProtocolError, as in

begin
header, data = http.get(URL)
rescue Net::ProtocolError
STDERR.puts “HTTP retrieval failed”
end

Gavin

···

From: “Basti S” basti.steiner@gmx.de

Line 71 of /usr/local/lib/ruby/1.6/net/http.rb is your friend!

=== Redirect

require 'net/http'
Net::HTTP.version_1_1   # declear to use 1.1 features.

host = 'www.ruby-lang.org'
path = '/'
begin
  Net::HTTP.start( host, 80 ) {|http|
    response , = http.get(path)
    print response.body
  }
rescue Net::ProtoRetriableError => err
  if m = %r<http://([^/]+)>.match( err.response['location'] ) then
    host = m[1].strip
    path = m.post_match
    retry
  end
end
···

Basti S (basti.steiner@gmx.de) wrote:

Hi,
i’m usin ver 1.6.7 of ruby . Everytime I try to access a site via Net::HTTP
and this site has a 302 response code it says

/net/http.rb:1432: in error! : 302 “Found” (Net::ProtoRetriableError)

Ruby terminates the script with this errormessage. But I want to read out
the new location of the url. Does anybody know how to get this url or if
there is any possibility to solve this problem?


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04