7

I'm using this code to scraping external html files

link = URI.parse(url)
request = Net::HTTP::Get.new(link.path)
response = Net::HTTP.start(link.host, link.port) {|http|
  http.request(request)
}

Works great but with slowed web pages sometimes responds timeout, so I need set a timeout limit per connection. Any idea?

skozz
  • 2,662
  • 3
  • 26
  • 37
  • possible duplicate of [How to specify a read timeout for a Net::HTTP::Post.new request in Ruby 2](http://stackoverflow.com/questions/19547184/how-to-specify-a-read-timeout-for-a-nethttppost-new-request-in-ruby-2) same for get and post – Ciro Santilli OurBigBook.com Oct 11 '14 at 15:23

1 Answers1

14

You need to set the read_timeout attribute.

link = URI.parse(url)
request = Net::HTTP::Get.new(link.path)
begin
  response = Net::HTTP.start(link.host, link.port) {|http|
    http.read_timeout = 100 #Default is 60 seconds
    http.request(request)
  }
rescue Net::ReadTimeout => e  
   puts e.message
end
Kumar Akarsh
  • 4,954
  • 2
  • 20
  • 31
  • Works!, perfect but now I need a response to return `false`or `true`depends of the timeout's response. Like `if response = not responding then return false` You know? – skozz Jan 07 '14 at 10:21
  • 1
    You need to handle the timeout exception. I have made changes to handle the exception in case of timeout. – Kumar Akarsh Jan 07 '14 at 10:36
  • And if you think the answer was satisfactory enough, please remember to tick the answer correct :). – Kumar Akarsh Jan 07 '14 at 10:38
  • Just one word: awesome. Thank man. Of course I will mark the answer as correct. – skozz Jan 07 '14 at 16:28
  • 1
    Btw! Ruby 2 are changed this parameters: http://ruby-doc.org/stdlib-2.1.0/libdoc/net/http/rdoc/Net/HTTP.html#open_timeout-attribute-method I'll update your reply :D – skozz Jan 08 '14 at 17:43
  • I'm simply using Net::HTTP.get(URI.parse(s)) - everything in one line. Anyway I can add a time out parameter? – Nathan B Feb 02 '21 at 20:18