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'中
来自c:/ruby/lib/ruby/1.8/timeout.rb:56:in timeout'
from c:/ruby/lib/ruby/1.8/timeout.rb:76:in
timeout'
来自c:/ruby/lib/ruby/1.8/net/protocol.rb:132:in 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'
来自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'
来自c:/ruby/lib/ruby/1.8/net/http.rb:547:in 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:在`get_print'中
来自urlparsing1.rb:3
答案 0 :(得分:0)
我无法在现代红宝石版本上重现上述行为,但我想我知道是什么导致它。
让我们一步一步地进入执行过程:
▶ 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]
总结:我猜你应该自己处理重定向。
答案 1 :(得分:0)
你可以试试这个:
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