Ruby - increasing proxy request timeout

Deadly 提交于 2019-12-08 03:08:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!