簡體   English   中英

EOFError:文件結尾與Net :: HTTP達成問題

[英]EOFError: end of file reached issue with Net::HTTP

我使用的是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

這是錯誤:

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

這似乎只在Facebook API的情況下發生。 另外,我在一些帖子中看到它可能是Net :: HTTP中的一個錯誤。

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

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

請注意附加的http.use_ssl = true

而處理http和https的更合適的代碼將類似於以下代碼。

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

在我的博客中查看更多內容: EOFError:使用Net :: HTTP發布表單時文件結束問題

我對非SSL服務的請求有類似的問題。

這篇博客模糊地建議嘗試URI編碼傳遞給'get'的URL: http//www.loudthinking.org/2010/02/ruby-eoferror-end-of-file-reached.html

基於絕望,我對它進行了拍攝,在我的限制測試中,這似乎已經為我修復了它。 我的新代碼是:

@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) 

請注意,我使用@ http.start,因為我想在多個請求上維護HTTP會話。 除此之外,您可能想嘗試最相關的部分,即:get調用中的URI.encode(url)

我有同樣的問題,ruby-1.8.7-p357,並且徒勞地試了很多東西......

我終於意識到它只發生在使用相同XMLRPC :: Client實例的多個調用中!

所以現在我在每次調用時重新實例化我的客戶端它只是工作:|

我發現我會定期遇到這樣的Net :: HTTP和Net :: FTP問題,當我這樣做時,用timeout()來調用所有這些問題都會消失。 所以這會偶爾掛起3分鍾左右,然后提出一個EOFError:

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

這總是為我修好:

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

在做了一些研究之后,這發生在Ruby的XMLRPC::Client庫中 - 它使用NET::HTTP 客戶端使用NET::HTTPstart()方法,該方法保持連接為將來的請求打開。

這恰好發生在最后一次請求后的30秒 - 所以我的假設是它所服務的服務器在此之后關閉請求。 我不確定NET::HTTP的默認值是什么,以保持請求打開 - 但我要測試60秒,看看是否能解決問題。

我最近遇到了這個問題,最終發現這是由我們遇到的端點的網絡超時引起的。 幸運的是,我們能夠增加超時持續時間。

為了驗證這是我們的問題(實際上並不是net http的問題),我用curl做了同樣的請求並確認請求被終止。

在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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM