2009-12-09 48 views

回答

4

有几种方法,但最简单的可能是OpenURIThis blog post有一些示例代码,也通过Net :: HTTP(与Hpricot)和Rio。

2

简单...

response = Net::HTTP.get_response(URI.parse("yourURI")) 
8
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) 
    } 
} 
7

您可以使用开放式的URI,这是一个内衬

require 'open-uri' 

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

可能下载文件的最短途径:

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

谢谢@Clemens,这个解决方案Just Worked。你也可以考虑在这里回答这个问题:https://stackoverflow.com/questions/2263540/how-do-i-download-a-binary-file-over-http – 2015-06-19 00:10:00

相关问题