Checking for Errors and Redirects : HTTP Reponse « Network « Ruby






Checking for Errors and Redirects


require 'net/http'

def get_web_document(url)
  uri = URI.parse(url)
  response = Net::HTTP.get_response(uri)

  case response
    when Net::HTTPSuccess:
      return response.body
    when Net::HTTPRedirection:
      return get_web_document(response['Location'])
    else
      return nil
  end
end

puts get_web_document('http://www.rubyinside.com/test.txt')
puts get_web_document('http://www.rubyinside.com/non-existent')

 








Related examples in the same category

1.Grabbing the Contents of a Web Page.rb
2.Get response encoding and body
3.Get url and response
4.Get response code
5.get server information from the response
6.Each key in the response
7.For each segment received
8.Zip header
9.Read mysql command output
10.Read a web page