Say, I thought I had this working at one point, but I don’t now seem to
be
able to feed a user agent header to the get_response, nor the get method
now.
Presumably I was mistaken to believe I ever had it. Can someone make a
suggestion as to how I can feed an arbitrary HTTP header (most
especially a
UAH) to these methods? Thanks. My present code is as follows:
#!/usr/bin/ruby
require ‘net/http’
require ‘uri’
def fetch(uri_str, limit=10)
fail ‘http redirect too deep’ if limit.zero?
puts “Trying: #{uri_str}”
begin
uah = <<EOUAH
Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.7.10) Gecko/20050716
Firefox/1.0.6
EOUAH
response = Net::HTTP.get_response(URI.parse(uri_str),{‘User-Agent’
=> uah})
case response
when Net::HTTPSuccess
return response
when Net::HTTPRedirection
response = fetch(response[‘location’],limit-1)
return response
else
response.error!
end
rescue Timeout::Error, EOFError => kapow
STDERR.print “trace Net::HTTP::get failure: #{uri_str}\n#{@uah}\n”
STDERR.print “trace Net::HTTP::get tc: #{tryCount}\n”
STDERR.print “trace Net::HTTP::get kapow: #{kapow}\n”
end
end
response = fetch(‘http://www.stage.rhapsody.com/genre?genreId=115’)
p response.body[500,55]
I’ve tried several variasious on this, but this modification of the
Pickaxe
example illustrates most closely the set of things I’m trying to
accomplish.
Seems to me this should be pretty simple, right? Also, here is my ruby
-v:
ruby 1.8.3 (2005-06-23) [i486-linux]
xc