title: Ruby - how to download a file if the url is a redirection?
body: <p>Ruby how to download a file if the url is a redirection?</p>
  
  <p>i'm trying to download this url:</p>
  
  <p>soundcloud.com/stereo-f---/cohete-amigo/download</p>
  
  <p>the redirection is this:</p>
  
  <p>[ec-media.soundcloud.com/HNIGsuMJlDhy?ff61182e3c2ecefa438cd0210ad0e38569b9775ddc9e06b3c362a686319250ea5c1ae2d33d8d525807641f258e33de3cb0e559c1b591b5b00fb32d5ef9&amp;AWSAccessKeyId=AKIAJ4IAZE5EOI7PA7VQ&amp;Expires=1352919869&amp;Signature=OVWD9VdV7ew%2B%2Fs%2BO0YpkKZLGOCw%3D][2]</p>
  
  <p>and what i've tried is this:</p>
  
  <pre><code>Net::HTTP.start("ec-media.soundcloud.com") { |http|
    resp = http.get("/HNIGsuMJlDhy?ff61182e3c2ecefa438cd0210ad0e38569b9775ddc9e06b3c362a686319250ea5c1ae2d33d8d525807641f258e33de3cb0e559c1b591b5b00fb32d5ef9&amp;amp;AWSAccessKeyId=AKIAJ4IAZE5EOI7PA7VQ&amp;amp;Expires=1352919869&amp;amp;Signature=OVWD9VdV7ew%2B%2Fs%2BO0YpkKZLGOCw%3D")
    open("test.wav", "wb") { |file|
      file.write(resp.body)
    }
  }
  puts "Done."
  sleep 50
  </code></pre>
  
  <p>i don't understand/know ANY of internet protocols and redirections and that things... please can give me a good explanation or help with my script to download files from soundcloud with ruby?</p>
  
  <p>thankyou</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>I've tried this way but i get an error:</p>
  
  <pre><code>Net::HTTP.start("soundcloud.com") do |http|
    resp = http.get("/dubstep-4/kill-paris-tender-love/download")
  
    while resp.code == '301' || resp.code == '302'
      # moved permanently or temporarily:  try again at the new location.
      resp = http.get(URI.parse(resp.header['location']))
      # ideally you should also bail if you make too many redirects.
    end
  
    # make sure the request was successful.
    if resp.code == '200'  
      open("test.wav", "wb"){ |file| file.write(resp.body) }
    else
      puts "Error: HTTP #{resp.code}"
    end
  end
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:1860:in `initialize': undefined method `empty?' for #&lt;URI::HTTP:0x25a9ce8&gt; (NoMethodError)
          from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:2093:in `initialize'
          from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:1026:in `new'
          from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:1026:in `get'
          from C:/Users/Administrador/Desktop/1.rb:59:in `block in &lt;main&gt;'
          from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:745:in `start'
          from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:557:in `start'
          from C:/Users/Administrador/Desktop/1.rb:48:in `&lt;main&gt;'
  </code></pre>