# How to retry a disrupted Net::HTTP download?

**URL:** <https://www.ruby-forum.com/t/how-to-retry-a-disrupted-net-http-download/105893>\
**Category:** Ruby\
**Created:** [June 4, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/how-to-retry-a-disrupted-net-http-download/105893 "2007-06-04T13:37:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [June 4, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/how-to-retry-a-disrupted-net-http-download/105893/1 "2007-06-04T13:37:02Z")

</div>

Hi,  
I’m trying to download a file with Ruby from a website. Everything works  
fine, but I can’t get Ruby to retry the download when for example the  
internet connection breaks during the download.

Here’s my code so far (it’s easier to read in a pastie):  
[http://pastie.caboo.se/67574](http://pastie.caboo.se/67574)

The Timeout::Error exception never happens, even though it should time  
out after 5 seconds. The funny thing is, if I deactivate my internet and  
then reconnect it, it does continue the download of the file, even  
though the commands under “rescue Timeout::Error” aren’t executed.

I hope someone can help me, I googled for an hour and tried tons of  
different things that I found in the API docs, but I’m pretty stuck  
right now.

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [June 5, 2007, 11:39am UTC](https://www.ruby-forum.com/t/how-to-retry-a-disrupted-net-http-download/105893/2 "2007-06-05T11:39:49Z")

</div>

Okay I found it out by myself, here’s the working code if someone else  
ever wants to do this:  
[http://pastie.caboo.se/67910](http://pastie.caboo.se/67910)

The problem was that Ruby already uses a timeout for the Net::HTTP class  
and because of this, you can’t wrap another timeout around it. But you  
can set the timeout Ruby uses with http.read\_timeout and  
http.open\_timeout. This way I was able to handle the Timeout::Error and  
now everything works just fine.

Chris Colon wrote:

> Hi,  
> I’m trying to download a file with Ruby from a website. Everything works  
> fine, but I can’t get Ruby to retry the download when for example the  
> internet connection breaks during the download.
> 
> Here’s my code so far (it’s easier to read in a pastie):  
> [Parked at Loopia](http://pastie.caboo.se/67574)
> 
> The Timeout::Error exception never happens, even though it should time  
> out after 5 seconds. The funny thing is, if I deactivate my internet and  
> then reconnect it, it does continue the download of the file, even  
> though the commands under “rescue Timeout::Error” aren’t executed.
> 
> I hope someone can help me, I googled for an hour and tried tons of  
> different things that I found in the API docs, but I’m pretty stuck  
> right now.
