tags:

views:

153

answers:

1

I want to fetch some content from a webserver using Net:HTTP, like this:

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

But I need to limit the get to the first 5kb to reduce the network traffic. How do I do this?

+2  A: 

I'm not sure when using Net::HTTP but using OpenURI i usually do the following:

require 'open-uri'

resource = open('http://google.com')

resource.read( 5120 ) 
=> # reads first 5120 characters, which i'm assuming would be 5KB.

hope this helps.

ucron