我正在尝试使用 ruby 脚本通过 http 链接下载 .dmg 文件。我已经尝试过http://www.ruby-doc.org/stdlib-2.0.0/libdoc/net/http/rdoc/Net/HTTP.html中描述的 NET::HTTP 和 HTTParty 方法 这些方法适用于图像和其他网页内容,但不适用于 dmg 文件。
#!/usr/bin/env ruby
require 'fileutils'
require 'pathname'
require "open-uri"
require 'net/http'
include FileUtils
#download Software
#Method1
open("VirtualBox-4.2.18-88780-OSX.dmg") do |file|
file.write('http://download.virtualbox.org/virtualbox/4.2.18/VirtualBox-4.2.18-88780-OSX.dmg').read
end
#Method2
Net::HTTP.start("download.virtualbox.org") do |http|
resp = http.get("/virtualbox/4.2.18/VirtualBox-4.2.18-88780-OSX.dmg")
open("VirtualBox-4.2.18-88780-OSX.dmg","wb") do |file|
file.write(resp)
end
end
#Method3
uri = URI('http://download.virtualbox.org/virtualbox/4.2.18/VirtualBox-4.2.18-88780- OSX.dmg')
Net::HTTP.get(uri)
#Method4
Net::HTTP.start(uri.host) do |http|
request = Net::HTTP::Get.new uri
response = http.request request # Net::HTTPResponse object
end
#Method5
File.open("~/Desktop/", "wb") do |f|
f.write HTTParty.get("http://download.virtualbox.org/virtualbox/4.2.18/VirtualBox-4.2.18- 88780-OSX.dmg").parsed_response
end
#Method6
File.write('VirtualBox-4.2.18-88780-OSX.dmg', open('http://download.virtualbox.org/virtualbox/4.2.18/VirtualBox-4.2.18-88780-OSX.dmg').read, {mode: 'wb'})
#Methos7
puts "#{uri}"
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)
如何通过 http 链接获取 dmg 文件。