`sysread':到达文件结尾(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'中
我不能在现代ruby版本上重现上述行为,但我想我知道是什么原因造成的。
让我们一步一步潜入执行过程:
▶ u = URI.parse('https://forums.malwarebytes.org/index.php?showtopic=49893')
#⇒ #<URI::HTTPS https://forums.malwarebytes.org/index.php?showtopic=49893>
好的, URI
得到了。
▶ 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。 重定向。 让我们来处理它:
▶ 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]
总结:我想你应该自己处理重定向。
你可以尝试使用这个:
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/6605.html
上一篇: `sysread': end of file reached (EOFError)
下一篇: Trouble using cucumber/capybara/selenium with different browsers