开发者

How do I download a file over HTTP using Ruby?

开发者 https://www.devze.com 2022-12-14 02:37 出处:网络
How do I download a fi开发者_JS百科le over HTTP using Ruby?Probably the shortest way to download a file:

How do I download a fi开发者_JS百科le over HTTP using Ruby?


Probably the shortest way to download a file:

require 'open-uri'
download = open('http://example.com/download.pdf')
IO.copy_stream(download, '~/my_file.pdf')


You can use open-uri, which is a one liner

require 'open-uri'

content = open('http://example.com').read


require 'net/http'
#part of base library
Net::HTTP.start("your.webhost.com") { |http|
  resp = http.get("/yourfile.xml")
  open("yourfile.xml", "wb") { |file|
    file.write(resp.body)
  }
}


There are several ways, but the easiest is probably OpenURI. This blog post has some sample code, and also goes over Net::HTTP (with Hpricot) and Rio.


Simple...

response = Net::HTTP.get_response(URI.parse("yourURI"))
0

精彩评论

暂无评论...
验证码 换一张
取 消