ada91 ada91 - 3 months ago 8
HTTP Question

How to set a proxy in rubys net/http?

I'm trying to set a proxy and to use it in a simple get request like in the documentation.
But I always receive an error!
The adress and port are right with open-uri it worked.. it's

http://proxy:8080
.

proxy_addr = 'proxy'
proxy_port = 8080

Net::HTTP.new('google.de', nil, proxy_addr, proxy_port).start { |http|
# always proxy via your.proxy.addr:8080
Net::HTTP.get('google.de', '')
}


What am I doing wrong?
Thanks for all answers!

Answer
Net::HTTP.new('google.de', nil, proxy_addr, proxy_port).start { |http|

This will create an http object for you to use in the block. Use that rather than generating new ones each time, here Net::HTTP.get('google.de', '')

proxy_addr = 'proxy'
proxy_port = 8080

Net::HTTP.new('google.de', nil, proxy_addr, proxy_port).start { |http|
  # always proxy via your.proxy.addr:8080
  http.get('google.de', '')
}
Comments