[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Beginner Question with Net::Proxy

Richard Cole

4/7/2005 3:06:00 AM

Hi,

I'm trying without success to do an http get via a proxy using net/http
and I just can't work it out. Got any suggestions where I'm going wrong,
how the code could be made simpler? Are there any decent docs for the
library net library? My non working code follows.

I redefined the InternetMessageIO method connect to see what the
net/http library is doing and it isn't using the proxy settings.

regards,

Richard.


-----

require 'net/http'
require 'cgi'

class HttpGetError < Exception
attr_reader :response
def initialize(response)
@response = response
end
end

module Net

class InternetMessageIO

def connect( open_timeout )
puts "LOG: opening connection to #{@address}..."
timeout(open_timeout) {
@socket = TCPsocket.new(@address, @port)
}
@rbuf = ''
end

end

end


class PubMed

attr_reader :proxy_addr, :proxy_port, :proxy

def initialize
@einfo = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/einfo....
@proxy_addr = "proxy" # this is my local proxy
@proxy_port = 80
@proxy = Net::HTTP::Proxy(@proxy_address, @proxy_port)
end

def query(base, cgi_args)
uri = URI.parse(base)
cgi_arg_list = cgi_args.collect { |h,v| h + "=" + CGI.escape(v) }
uri.query = cgi_arg_list.join("&")

puts "Fetching .... " + uri.to_s
@proxy.start(uri.host, uri.port) { |http|
resp = @proxy.get(uri.path + "?" + uri.query)
puts resp.class
resp = http.get(uri)
if resp.message == "OK" then
return resp.data
else
raise(HttpGetError.new(response), "Http Get Failed: " +
response.to_s)
end
}
end

def databases
return query(@einfo, {})
end

end

pubmed = PubMed.new
puts "Databases:"
puts pubmed.databases



1 Answer

Assaph Mehr

4/7/2005 3:56:00 AM

0


> class PubMed
>
> attr_reader :proxy_addr, :proxy_port, :proxy
>
> def initialize
> @einfo =
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/einfo....
> @proxy_addr = "proxy" # this is my local proxy
> @proxy_port = 80
> @proxy = Net::HTTP::Proxy(@proxy_address, @proxy_port)
^^^^^^^^^^^^^^
Surely you meant @proxy_addr ?...


> end
>
> def query(base, cgi_args)
> uri = URI.parse(base)
> cgi_arg_list = cgi_args.collect { |h,v| h + "=" + CGI.escape(v) }
> uri.query = cgi_arg_list.join("&")
>
> puts "Fetching .... " + uri.to_s
> @proxy.start(uri.host, uri.port) { |http|

of these lines:

> resp = @proxy.get(uri.path + "?" + uri.query)
> puts resp.class
> resp = http.get(uri)

you dont need this line - you should only use the http object.
resp = http.get(uri.path + "?" + uri.query)
puts resp.class


HTH,
Assaph