Ruby Net:HTTP SSL错误还是我?

时间:2011-04-19 19:21:17

标签: ruby-on-rails ruby ssl facebook-graph-api

我正在尝试使用ruby下载https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/174043_1701680_6450592_q.jpg,但是出现连接重置错误。它可以在浏览器中下载,但不能在Ruby中下载。这是Ruby中的错误吗? Facebook的一个错误?

这适用于大多数其他Facebook用户。我使用的是Ruby 1.8.7和rails 2.3.11。

>> http = Net::HTTP.new("graph.facebook.com", Net::HTTP.https_default_port)
=> #<Net::HTTP graph.facebook.com:443 open=false>
>> http.use_ssl = true
=> true
>> http.start do |http|
?> response = Net::HTTP.get_response(URI.parse('https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/174043_1701680_6450592_q.jpg'))
>> end
warning: peer certificate won't be verified in this SSL session
Errno::ECONNRESET: An existing connection was forcibly closed by the remote host.
        from c:/Ruby187/lib/ruby/1.8/net/protocol.rb:135:in `sysread'
        from c:/Ruby187/lib/ruby/1.8/net/protocol.rb:135:in `rbuf_fill'
        from c:/Ruby187/lib/ruby/1.8/timeout.rb:67:in `timeout'
        from c:/Ruby187/lib/ruby/1.8/timeout.rb:101:in `timeout'
        from c:/Ruby187/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
        from c:/Ruby187/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
        from c:/Ruby187/lib/ruby/1.8/net/protocol.rb:126:in `readline'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:2028:in `read_status_line'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:2017:in `read_new'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:1051:in `request'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:948:in `request_get'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:380:in `get_response'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:543:in `start'
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:379:in `get_response'
        from (irb):5
        from c:/Ruby187/lib/ruby/1.8/net/http.rb:543:in `start'
        from (irb):4>>

1 个答案:

答案 0 :(得分:4)

这是一个稍微修改过的版本......

require 'uri'
require 'net/https'

url = URI.parse 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/174043_1701680_6450592_q.jpg'
http = Net::HTTP.new url.host, url.port
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true

http.start do |agent|
  p agent.get(url.path).read_body.length
end

然后......

    so ross$ ruby fbi
    1974
相关问题