gpt4 book ai didi

ruby - 如何通过 Net::HTTP::Get 下载二进制文件?

转载 作者:数据小太阳 更新时间:2023-10-29 08:49:36 24 4
gpt4 key购买 nike

我正在尝试使用以下 Ruby 脚本通过 HTTP 下载二进制文件。

#!/usr/bin/env ruby
require 'net/http'
require 'uri'

def http_download(resource, filename, debug = false)
uri = URI.parse(resource)
puts "Starting HTTP download for: #{uri}"
http_object = Net::HTTP.new(uri.host, uri.port)
http_object.use_ssl = true if uri.scheme == 'https'
begin
http_object.start do |http|
request = Net::HTTP::Get.new uri.request_uri
Net::HTTP.get_print(uri) if debug
http.read_timeout = 500
http.request request do |response|
open filename, 'w' do |io|
response.read_body do |chunk|
io.write chunk
end
end
end
end
rescue Exception => e
puts "=> Exception: '#{e}'. Skipping download."
return
end
puts "Stored download as #{filename}."
end

但是它下载的是 HTML 源代码而不是二进制文件。当我在浏览器中输入 URL 时,二进制文件被下载。这是脚本失败的 URL:

http://dcatlas.dcgis.dc.gov/catalog/download.asp?downloadID=2175&downloadTYPE=KML

我执行脚本如下

pry> require 'myscript'
pry> resource = "http://dcatlas.dcgis.dc.gov/catalog/download.asp?downloadID=2175&downloadTYPE=KML"
pry> http_download(resource,"StreetTreePt.KML", true)

如何下​​载二进制文件?

重定向实验

我找到了这个 redirection check这看起来很合理。当我集成到响应 block 中时,它失败并出现以下错误:

Exception: 'undefined method `host' for "save_download.asp?filename=StreetTreePt.KML":String'. Skipping download.

在上面贴出的“原始”函数中没有发生异常。

最佳答案

Net::HTTP 的文档显示 how to handle redirects :

Following Redirection

Each Net::HTTPResponse object belongs to a class for its response code.

For example, all 2XX responses are instances of a Net::HTTPSuccess subclass, a 3XX response is an instance of a Net::HTTPRedirection subclass and a 200 response is an instance of the Net::HTTPOK class. For details of response classes, see the section “HTTP Response Classes” below.

Using a case statement you can handle various types of responses properly:

def fetch(uri_str, limit = 10)
# You should choose a better exception.
raise ArgumentError, 'too many HTTP redirects' if limit == 0

response = Net::HTTP.get_response(URI(uri_str))

case response
when Net::HTTPSuccess then
response
when Net::HTTPRedirection then
location = response['location']
warn "redirected to #{location}"
fetch(location, limit - 1)
else
response.value
end
end

print fetch('http://www.ruby-lang.org')

或者,您可以使用 Ruby 的 OpenURI , 它会自动处理。或者,Curb gem 会做到的。大概 TyphoeusHTTPClient也是。

根据您在问题中显示的代码,您获得的异常只能来自:

http_object = Net::HTTP.new(uri.host, uri.port)

这不太可能,因为 uri 是一个 URI 对象。如果您需要帮助解决该问题,则需要显示完整的代码。

关于ruby - 如何通过 Net::HTTP::Get 下载二进制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16616713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com