2014-02-12 77 views
0

我試圖做這樣一件趣事:http.request不斷給我EOFError?

http = Net::HTTP.new("t66y.com", 80) 
request = Net::HTTP::Get.new("http://t66y.com/") 
response = http.request(request) 
puts response.inspect 

它工作得很好,給我<Net::HTTPOK 200 OK readbody=true>。但是,在我將url更改爲類似http://t66y.com/thread0806.php?fid=16之後,它會一直向我投擲EOFError異常。整個日誌是:

/Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock': end of file reached (EOFError) 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/protocol.rb:141:in `rbuf_fill' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/protocol.rb:92:in `read' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:2779:in `ensure in read_chunked' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:2779:in `read_chunked' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:2750:in `read_body_0' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:2710:in `read_body' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:2735:in `body' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:2672:in `reading_body' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:1321:in `block in transport_request' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:1316:in `catch' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:1293:in `request' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:1286:in `block in request' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:745:in `start' 
    from /Users/lei/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/net/http.rb:1284:in `request' 
    from /Users/lei/workspace/Dadiaosi/scraper.rb:18:in `<top (required)>' 
    from -e:1:in `load' 
    from -e:1:in `<main>' 

你們有什麼線索嗎?

回答

1

這些工作:

在終端:

$ curl -v http://t66y.com/thread0806.php?fid=16 

在紅寶石:

require 'open-uri' 
response = open("http://t66y.com/thread0806.php?fid=16") 
html = response.read 

從捲曲響應我可以看到標題和內容長度缺失和charset是中國人。如果你使用的是舊版本的ruby,那麼這可能會導致ruby net http庫失靈。

您可以輕鬆地在open-uri中進行交換,以獲取如上所示的html。

+0

Thanks.After我升級到ruby2.0,這個問題似乎消失了。 – LeoShi

0

應該

uri = URI('http://t66y.com/thread0806.php?fid=16') 
response = Net::HTTP.get(uri)