Ruby-on-rails EOFError:文件末尾到达问题 Net::HTTP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5244887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:37:04  来源:igfitidea点击:

EOFError: end of file reached issue with Net::HTTP

ruby-on-railsrubyweb-servicesfacebook-fql

提问by Rahul Singh

I am using ruby-1.8.7-p302/Rails 2.3.11. I am trying to use FQL (Facebook API) to get stats for a link. Here's my code:

我正在使用 ruby​​-1.8.7-p302/Rails 2.3.11。我正在尝试使用 FQL (Facebook API) 来获取链接的统计信息。这是我的代码:

def stats(fb_post_url)
  url = BASE_URI + "?query=#{URI.encode("select like_count from link_stat where url=\"#{fb_post_url}\"")}"
  parsed_url = URI.parse(url)
  http = Net::HTTP.new(parsed_url.host, parsed_url.port)
  request = Net::HTTP::Get.new(parsed_url.request_uri)

  response = http.request(request)
  response.inspect
end

And here's the error:

这是错误:

EOFError: end of file reached
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/protocol.rb:135:in `sysread'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/protocol.rb:135:in `rbuf_fill'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/timeout.rb:67:in `timeout'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/timeout.rb:101:in `timeout'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/protocol.rb:126:in `readline'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:2028:in `read_status_line'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:2017:in `read_new'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:1051:in `request'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:1037:in `request'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:543:in `start'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:1035:in `request'
from /home/rahul/Work/Radr/lib/fb_stats.rb:13:in `stats'
from (irb):10

This seems to be happening only in case of the Facebook API. Also, I saw it suggested in some post this could be a bug in Net::HTTP.

这似乎只发生在 Facebook API 的情况下。另外,我在一些帖子中看到它建议这可能是 Net::HTTP 中的一个错误。

回答by Sur Max

If the URL is using https instead of http, you need to add the following line:

如果 URL 使用 https 而不是 http,则需要添加以下行:

parsed_url = URI.parse(url)
http = Net::HTTP.new(parsed_url.host, parsed_url.port)
http.use_ssl = true

Note the additional http.use_ssl = true.

注意额外的http.use_ssl = true.

And the more appropriate code which would handle both http and https will be similar to the following one.

处理 http 和 https 的更合适的代码将类似于以下代码。

url = URI.parse(domain)
req = Net::HTTP::Post.new(url.request_uri)
req.set_form_data({'name'=>'Sur Max', 'email'=>'[email protected]'})
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == "https")
response = http.request(req)

See more in my blog: EOFError: end of file reached issue when post a form with Net::HTTP.

在我的博客中查看更多信息:EOFError:使用 Net::HTTP 发布表单时文件结束到达问题

回答by Phil

I had a similar problem with a request to a non-SSL service.

我在请求非 SSL 服务时遇到了类似的问题。

This blog suggested vaguely to try URI encoding the URL that is passed to 'get': http://www.loudthinking.org/2010/02/ruby-eoferror-end-of-file-reached.html

该博客含糊地建议尝试对传递给“get”的 URL 进行 URI 编码:http: //www.loudthinking.org/2010/02/ruby-eoferror-end-of-file-reached.html

I took a shot at it, based on desperation, and in my limiting testing this seems to have fixed it for me. My new code is:

我出于绝望尝试了它,在我的有限测试中,这似乎为我解决了这个问题。我的新代码是:

@http = Net::HTTP.new('domain.com')  
@http = @http.start    
url = 'http://domain.com/requested_url?blah=blah&etc=1'
req = Net::HTTP::Get.new(URI.encode(url))
req.basic_auth USERNAME, API_KEY
res = @http.request(req) 

Note that I use @http.start as I want to maintain the HTTP session over multiple requests. Other than that, you might like to try the most relevant part which is: URI.encode(url)inside the get call

请注意,我使用 @http.start 是因为我想在多个请求上维护 HTTP 会话。除此之外,您可能想尝试最相关的部分:get 调用中的URI.encode(url)

回答by jobwat

I had the same problem, ruby-1.8.7-p357, and tried loads of things in vain...

我遇到了同样的问题,ruby-1.8.7-p357,并且尝试了很多东西都是徒劳的......

I finally realised that it happens only on multiple calls using the same XMLRPC::Client instance!

我终于意识到它只发生在使用同一个 XMLRPC::Client 实例的多次调用中!

So now I'm re-instantiating my client at each call and it just works:|

所以现在我在每次调用时重新实例化我的客户端,它只是有效:|

回答by Sean

I find that I run into Net::HTTP and Net::FTP problems like this periodically, and when I do, surrounding the call with a timeout() makes all of those issues vanish. So where this will occasionally hang for 3 minutes or so and then raise an EOFError:

我发现我会定期遇到这样的 Net::HTTP 和 Net::FTP 问题,当我这样做时,用 timeout() 包围调用会使所有这些问题消失。因此,这偶尔会挂起 3 分钟左右,然后引发 EOFError:

res = Net::HTTP.post_form(uri, args)

This always fixes it for me:

这总是为我修复它:

res = timeout(120) { Net::HTTP.post_form(uri, args) }

回答by TJ Biddle

After doing some research, this was happening in Ruby's XMLRPC::Clientlibrary - which uses NET::HTTP. The client uses the start()method in NET::HTTPwhich keeps the connection open for future requests.

在做了一些研究之后,这发生在 Ruby 的XMLRPC::Client库中——它使用NET::HTTP. 客户端使用保持连接打开以供将来请求使用的start()方法NET::HTTP

This happened precisely at 30 seconds after the last requests - so my assumption here is that the server it's hitting is closing requests after that time. I'm not sure what the default is for NET::HTTPto keep the request open - but I'm about to test with 60 seconds to see if that solves the issue.

这恰好发生在最后一次请求之后的 30 秒 - 所以我在这里的假设是它正在击中的服务器在那段时间之后关闭请求。我不确定NET::HTTP保持请求打开的默认设置- 但我将测试 60 秒,看看是否能解决问题。

回答by Tyler

I ran into this recently and eventually found that this was caused by a network timeout from the endpoint we were hitting. Fortunately for us we were able to increase the timeout duration.

我最近遇到了这个问题,最终发现这是由我们正在访问的端点的网络超时引起的。幸运的是,我们能够增加超时持续时间。

To verify this was our issue (and actually not an issue with net http), I made the same request with curl and confirmed that the request was being terminated.

为了验证这是我们的问题(实际上不是 net http 的问题),我使用 curl 发出了相同的请求并确认请求已被终止。

回答by Nicolas Grenié

In Ruby on Rails I used this code, and it works perfectly:

在 Ruby on Rails 中,我使用了这段代码,它完美运行:

req_profilepic = ActiveSupport::JSON.decode(open(URI.encode("https://graph.facebook.com/me/?fields=picture&type=large&access_token=#{fb_access_token}")))

profilepic_url = req_profilepic['picture']