From: Erik Van eykelen Date: 2007-04-04T15:34:12+09:00 Subject: Re: Net::HTTP.get_response hangs with particular url Dan Zwell wrote: > I'm not positive, but I would point out that the URL in question is an > 82MB mp3. I was under the impression that getting a HTTPResponse > actually downloaded the body of the requested page/file. So perhaps the > response is just taking a long time in arriving? My connection would > need more than 10 minutes for this to return (though I admit my error is > different when I press control-c) > > Dan Hi Dan, My connection to this file seems much worse than yours, I just tried it with curl and it took 50 minutes! That's why it took so incredibly long! By the way, I came across this issue while looking for a clean and simple way to (1) catch redirects and (2) test whether an object is available for download on a certain location. I hereby post a snippet who does just that, maybe it helps other folks who are looking for this solution and stumble on long waits for get_response() to finish: require 'open-uri' require 'timeout' def test( _url ) begin content_length = nil bytes_read = nil Timeout::timeout( 10 ) do | length | open( _url, "User-Agent" => "test", :content_length_proc => lambda { |cl| content_length = cl }, :progress_proc => lambda { |br| bytes_read = br raise HadEnoughException if bytes_read > 0 } ) end rescue HadEnoughException puts "HadEnoughException" rescue Timeout::Error puts "Timed out on [#{_url}]" rescue OpenURI::HTTPError puts "Cannot open [#{_url}]" rescue Exception => e puts "Exception #{e.message}" end return content_length, bytes_read end class HadEnoughException < Exception end content_length, bytes_read = test('http://apple.com') puts content_length, bytes_read # outputs HadEnoughException 31805 784 (values will be different on your computer) content_length, bytes_read = test('http://content.digitalwell.washington.edu/isilon/1/8/45/459ffc6f-3f91-46f5-af58-155791dad3b4.mp3') puts content_length, bytes_read # outputs HadEnoughException 86136581 769 (values will be different on your computer) Note that not all web servers return the content_length in the http header, so this value may be nil -- Posted via http://www.ruby-forum.com/.