Beginner Question with Net::Proxy

R

Richard Cole

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.fcgi"
@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
 
A

Assaph Mehr

class PubMed

attr_reader :proxy_addr, :proxy_port, :proxy

def initialize
@einfo = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/einfo.fcgi"
@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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top