From: Patrick Chanezon Date: 2006-04-22T02:47:27+09:00 Subject: Re: Problems trying to feed a user agent header to Net::HTTP.get_response ------=_Part_7416_3643987.1145641643975 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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 =3D ARGV.shift || "http://localhost/foo.cgi" clnt =3D HTTPAccess2::Client.new clnt.debug_dev =3D STDERR extheaders =3D [] clnt.set_cookie_store("cookie.dat") clnt.redirect_uri_callback =3D lambda { |res| uri =3D 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 '=3D GET content directly' puts clnt.get_content(target, nil, extheaders) puts '=3D GET result object' result =3D clnt.get(target) puts '=3D=3D Header object' p result.header puts "=3D=3D Content-type" p result.contenttype puts '=3D=3D Body object' p result.body puts '=3D=3D Content' print result.content puts '=3D=3D GET with Block' clnt.get(target) do |str| puts str end clnt.save_cookie_store On 4/21/06, Xeno Campanoli wrote: > > > Say, I thought I had this working at one point, but I don't now seem to b= e > 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=3D10) > fail 'http redirect too deep' if limit.zero? > puts "Trying: #{uri_str}" > begin > uah =3D < Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.7.10) Gecko/20050716 > Firefox/1.0.6 > EOUAH > response =3D Net::HTTP.get_response(URI.parse(uri_str),{'User-Agent' = =3D> > uah}) > case response > when Net::HTTPSuccess > return response > when Net::HTTPRedirection > response =3D fetch(response['location'],limit-1) > return response > # else > # response.error! > end > rescue Timeout::Error, EOFError =3D> 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 =3D fetch('http://www.stage.rhapsody.com/genre?genreId=3D115') > 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, AdWords API evangelist http://blog.chanezon.com/ http://www.google.com/apis/adwords/ ------=_Part_7416_3643987.1145641643975--