fork(4) download
  1. #!/usr/bin/ruby -w
  2. # answer: http://o...content-available-to-author-only...e.jp/qa/q7011589.html
  3.  
  4. require 'net/http'
  5. require 'uri'
  6.  
  7. uri_str = 'http://w...content-available-to-author-only...e.com/'
  8. uri = URI.parse uri_str
  9.  
  10. p uri
  11.  
  12. response = Net::HTTP.start uri.host {|http|
  13. http.get '/index.html'
  14. }
  15.  
  16. p response
  17. puts response
  18.  
  19. def fetch(uri_str, limit = 10)
  20. # You should choose better exception.
  21. raise ArgumentError, 'HTTP redirect too deep' if limit == 0
  22.  
  23. response = Net::HTTP.get_response(URI.parse(uri_str))
  24. case response
  25. when Net::HTTPSuccess
  26. response
  27. when Net::HTTPRedirection
  28. fetch(response['location'], limit - 1)
  29. else
  30. response.value
  31. end
  32. end
  33.  
  34. response2 = fetch uri_str
  35. p response2
  36. puts response2
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty