Wednesday, August 17, 2011

Net::HTTP and handling 302 Redirect


I meet two kinds of redirect: "Location" response and  tag.



Here's my solution:

================================================

require 'net/http'

require 'net/https'



http = Net::HTTP.new('www.microsoft.com', 80)

resp, data = http.get('/',nil)



#1."Location" response redirect...

if resp.response['Location']!=nil then

puts 'Direct to: ' + resp.response['Location']

redirectUrl=resp.response['Location']

end



#2. tag redirect...

redirectUrl=data.scan(//).to_s

if redirectUrl!=nil then

puts 'Direct to: ' +redirectUrl

end
http://www.ruby-forum.com/topic/142745

No comments:

Post a Comment