0

我正在实现具有 Facebook API 支持的 Rails 校友应用程序。其中一项要求是将应用程序中的消息直接发布到 facebook 墙上。一切似乎都很好,但是有一个我无法解决的问题。当我在大学工作时,我收到一个错误“无法建立连接,因为目标机器主动拒绝它”。这是因为我支持大学代理。我做了一些谷歌搜索并尝试了一些代码更改,但仍然得到相同的消息。

我可以完成这项工作的唯一方法是非常hacky。如果我将 http.rb 类中的方法签名从

def HTTP.new(地址,端口 = nil,p_addr = nil,p_port = nil,p_user = nil,p_pass = nil)
      h = 代理(p_addr,p_port,p_user,p_pass).newobj(地址,端口)
      h.instance_eval {
        @newimpl = ::Net::HTTP.version_1_2?
      }
      H
    结尾

def HTTP.new(地址,端口 = nil,p_addr = nil,p_port = nil,p_user = nil,p_pass = nil)
      h = 代理(“proxy.uni.ac.uk”,8080,p_user,p_pass).newobj(地址,端口)
      h.instance_eval {
        @newimpl = ::Net::HTTP.version_1_2?
      }
      H
    结尾

使用默认 http.rb 时,我得到的堆栈跟踪是

c:/ruby/lib/ruby/1.8/net/http.rb:565:in `initialize'
c:/ruby/lib/ruby/1.8/net/http.rb:565:in `open'
c:/ruby/lib/ruby/1.8/net/http.rb:565:in `connect'
c:/ruby/lib/ruby/1.8/timeout.rb:48:in `timeout'
c:/ruby/lib/ruby/1.8/timeout.rb:76:in `timeout'
c:/ruby/lib/ruby/1.8/net/http.rb:565:in `connect'
c:/ruby/lib/ruby/1.8/net/http.rb:558:in `do_start'
c:/ruby/lib/ruby/1.8/net/http.rb:547:in `start'
c:/ruby/lib/ruby/1.8/net/http.rb:404:in `post_form'
c:/ruby/lib/ruby/gems/1.8/gems/facebooker-1.0.54/lib/facebooker/service/net_http_service.rb:4:in `post_form'
c:/ruby/lib/ruby/gems/1.8/gems/facebooker-1.0.54/lib/facebooker/service.rb:78:in `post_form'
c:/ruby/lib/ruby/gems/1.8/gems/facebooker-1.0.54/lib/facebooker/service.rb:66:in `post'
c:/ruby/lib/ruby/gems/1.8/gems/facebooker-1.0.54/lib/facebooker/session.rb:610:in `post_without_logging'
c:/ruby/lib/ruby/gems/1.8/gems/facebooker-1.0.54/lib/facebooker/session.rb:621:in `post'
c:/ruby/lib/ruby/gems/1.8/gems/facebooker-1.0.54/lib/facebooker/logging.rb:20:in `log_fb_api'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:10:in `realtime'
c:/ruby/lib/ruby/gems/1.8/gems/facebooker-1.0.54/lib/facebooker/logging.rb:20:in `log_fb_api'
c:/ruby/lib/ruby/gems/1.8/gems/facebooker-1.0.54/lib/facebooker/session.rb:620:in `post'

我怎样才能使这项工作?

4

1 回答 1

1

如果您的校友应用服务器在大学防火墙内运行,请使用 Net::HTTP::Proxy 而不是 Net::HTTP

例如

Net::HTTP::Proxy("proxy.uni.ac.uk",8080).start( 'www.ruby-lang.org', 80 ) do |http|
     print( http.get( '/en/LICENSE.txt' ).body ) 
end

代替

Net::HTTP.start( 'www.ruby-lang.org',80 ) do |http| 
     print( http.get( '/en/LICENSE.txt' ).body )
end

参考:http ://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html > 通过代理访问

于 2010-06-07T05:21:27.113 回答