Forum: Ruby Problems trying to feed a user agent header to Net::HTTP.get

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Xeno C. (Guest)
on 2006-04-21 21:41
(Received via mailing list)
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
Patrick Chanezon (Guest)
on 2006-04-21 21:47
(Received via mailing list)
I had a similar issue lately (not sure it is similar): I wanted to feed
extra headers when redirected.
I use http-access2 instead of net/http
Here's a sample code that worked for me.

#!/usr/bin/ruby

require 'http-access2'
require 'pp'

target = ARGV.shift || "http://localhost/foo.cgi"

clnt = HTTPAccess2::Client.new
clnt.debug_dev = STDERR

extheaders = []

clnt.set_cookie_store("cookie.dat")
clnt.redirect_uri_callback = lambda { |res|
  uri = res.header['Location'][0]
  if res.header['set-cookie']
    res.header['set-cookie'].each do |cookie|
        puts cookie
        puts uri
        extheaders << ['Cookie', cookie]
        #bug in cookie manager, does not work, the uril for the cookie
misses the first element pof the path
#        clnt.cookie_manager.parse(cookie, URI.parse(uri))
        end
  end
  clnt.save_cookie_store
  pp clnt.cookie_manager
  URI.parse(uri)
}

puts
puts '= GET content directly'
puts clnt.get_content(target, nil, extheaders)
puts '= GET result object'
result = clnt.get(target)
puts '== Header object'
p result.header
puts "== Content-type"
p result.contenttype
puts '== Body object'
p result.body
puts '== Content'
print result.content
puts '== GET with Block'
clnt.get(target) do |str|
  puts str
end
clnt.save_cookie_store
This topic is locked and can not be replied to.