我正在使用Facebook API支持实现Rails校友应用程序。其中一个要求是将应用程序中的消息直接发布到Facebook墙。一切似乎都很好,但有一个问题我无法解决。当我在大学工作时,我收到一个错误“无法建立连接,因为目标机器主动拒绝它”。这是因为我落后于大学代理。我做了一些谷歌搜索并尝试了一些代码更改,仍然得到相同的消息。
我能做这项工作的唯一方法是非常hacky。 如果我从
更改http.rb类中的方法签名def HTTP.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) h = Proxy(p_addr, p_port, p_user, p_pass).newobj(address, port) h.instance_eval { @newimpl = ::Net::HTTP.version_1_2? } h end
到
def HTTP.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) h = Proxy("proxy.uni.ac.uk", 8080, p_user, p_pass).newobj(address, port) h.instance_eval { @newimpl = ::Net::HTTP.version_1_2? } h end
使用默认的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'
我该如何做到这一点?
答案 0 :(得分: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>通过代理访问