Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想使用 Net:HTTP 从网络服务器获取一些内容,如下所示:
url = URI.parse('http://www.example.com/index.html') res = Net::HTTP.start(url.host, url.port) {|http| http.get('/index.html') } puts res.body
但我需要将 get 限制在前 5kb 以减少网络流量。我该怎么做呢?
我不确定何时使用 Net::HTTP 但使用 OpenURI 我通常会执行以下操作:
require 'open-uri' resource = open('http://google.com') resource.read( 5120 ) => # reads first 5120 characters, which i'm assuming would be 5KB.
希望这可以帮助。