Ruby - 增加代理请求超时 [英] Ruby - increasing proxy request timeout

查看:67
本文介绍了Ruby - 增加代理请求超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过一些代理服务器访问一些内容,但我得到:

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

现在它不能识别 open_timeoutstart

Now it doesn't recagnize the open_timeout and start

undefined method `open_timeout=' for #<Net::HTTPOK 200 OK readbody=true>>
undefined method `start..

有什么帮助吗?

推荐答案

当你在 Proxy(HTTP) 类上调用 get_response 时,你会得到一个 Net::HTTPResponse实例,它不响应 startopen_timeout=.

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=.

使用 Net::HTTP::Proxy 创建代理 HTTP 类,创建该类的实例,然后修改该实例的超时设置.然后您可以使用该实例从代理后面获取内容.

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)

这篇关于Ruby - 增加代理请求超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆