问题
I'm trying to access some contents via some proxy servers but I get:
<Errno::ETIMEDOUT: Connection timed out - connect(2)>
I modified the code and tried to increase the timeout as below:
require 'open-uri'
require 'net/http'
response = Net::HTTP::Proxy(proxy_ip, proxy_port).get_response(uri.host, uri.path)
response.start(uri.host) do |http|
http.open_timeout = 5
http.read_timeout = 10
end
Now it doesn't recagnize the open_timeout and start
undefined method `open_timeout=' for #<Net::HTTPOK 200 OK readbody=true>>
undefined method `start..
Any help?
回答1:
When you call get_response on the Proxy(HTTP) class, you get a Net::HTTPResponse instance, and it doesn't respond to start or open_timeout=.
Use Net::HTTP::Proxy to create an proxied HTTP class, create an instance of that class, and then modify the timeout settings on that instance. And then you can use the instance to fetch contents from behind a proxy.
proxy_http = Net::HTTP.Proxy(proxy_ip, proxy_port).new(uri.host)
proxy_http.open_timeout = 5
proxy_http.read_timeout = 10
response = proxy_http.get(uri.path)
来源:https://stackoverflow.com/questions/16939969/ruby-increasing-proxy-request-timeout