`sysread': end of file reached (EOFError)

require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('https://forums.malwarebytes.org/index.php?showtopic=49893')

我得到一个错误,如下所示:: c:/ruby/lib/ruby/1.8/net/protocol.rb:133:在sysread': end of file reached (EOFError) from c:/ruby/lib/ruby/1.8/net/protocol.rb:133:in rbuf_fill'from c:/ruby/lib/ruby/1.8/timeout.rb:56:in timeout' from c:/ruby/lib/ruby/1.8/timeout.rb:76:in超时时间从c:/ruby/lib/ruby/1.8/net/protocol.rb:132:在rbuf_fill' from c:/ruby/lib/ruby/1.8/net/protocol.rb:116:in readuntil'从c:/ruby/lib/ruby/1.8/net/protocol.rb:126:in readline' from c:/ruby/lib/ruby/1.8/net/http.rb:2029:in read_status_line'from c:/ ruby / lib / ruby​​ / 1.8 / net / http.rb:2018:在read_new' from c:/ruby/lib/ruby/1.8/net/http.rb:1059:in c:/ ruby​​ / lib / ruby / 1.8 / net / http.rb:957:in request_get' from c:/ruby/lib/ruby/1.8/net/http.rb:380:in get_response'from c:/ruby/lib/ruby/1.8/ net / http.rb:547: start' from c:/ruby/lib/ruby/1.8/net/http.rb:379:in get_response中从c:/ruby/lib/ruby/1.8/net/http开始。 rb:337:来自urlparsing1.rb:3的`get_print'中


I can't reproduce the aforementioned behaviour on modern ruby version, but I think I know what causes it.

Let's dive into execution process step by step:

▶ u = URI.parse('https://forums.malwarebytes.org/index.php?showtopic=49893')
#⇒ #<URI::HTTPS https://forums.malwarebytes.org/index.php?showtopic=49893>

OK, URI got.

▶ Net::HTTP.get_print u
#⇒ <html><head><meta http-equiv='refresh' content='0;
#    url=/index.php?/topic/49893-removal-instructions-for-a-fast-antivirus/'>
#  </head><body></body></html>=> nil

Ooups. Redirect. Let's handle it:

▶ u_redir = Net::HTTP.get(u).scan(/url=(.*?)'/).first.first
#⇒ "/index.php?/topic/49893-removal-instructions-for-a-fast-antivirus/"

▶ u.path, u.query = u_redir.split '?'
#⇒ [
#  [0] "/index.php",
#  [1] "/topic/49893-removal-instructions-for-a-fast-antivirus/"
# ]
▶ Net::HTTP.get_print u
#⇒ [HTML CONTENT]

The summing up: I would guess you should handle redirects yourselves.


你可以尝试使用这个:

require 'net/http'
require 'uri'

uri = URI.parse('https://forums.malwarebytes.org/index.php?showtopic=49893')


request = Net::HTTP::Get.new uri.request_uri

res = Net::HTTP.start(uri.host, uri.port,
         :use_ssl => uri.scheme == 'https') {|http| http.request request}
p res.body
链接地址: http://www.djcxy.com/p/6606.html

上一篇: Rails没有找到步骤

下一篇: `sysread':到达文件结尾(EOFError)