简体   繁体   English

Rails应用程序行为代理

[英]Rails application behing proxy

I'm implementing Rails alumni application with Facebook API support. 我正在使用Facebook API支持实现Rails校友应用程序。 One of the requirements is to post a message from the application directly to facebook wall. 要求之一是将来自应用程序的消息直接发布到Facebook墙。 Everything seems to work fine, however there is one issue which I can't fix. 一切似乎都正常,但是有一个我无法解决的问题。 When I'm working at the University, I got an error "No connection could be made because the target machine actively refused it". 当我在大学工作时,出现错误“无法连接,因为目标计算机主动拒绝连接”。 This is because I'm behind University proxy. 这是因为我落后于大学代理。 I've done some googling and tried some changes in code and still got the same message. 我做了一些谷歌搜索,并尝试了一些代码更改,但仍然收到相同的消息。

The only way I can make this work is very hacky. 我做这项工作的唯一方法是非常棘手的。 If I change the method signature in http.rb class from 如果我从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

to

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

The stack trace I got, when using default http.rb is 使用默认的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'

How can I make this work? 我该如何进行这项工作?

Use Net::HTTP::Proxy instead of Net::HTTP if your alumni application server is running within the university firewall 如果校友应用服务器在大学防火墙内运行,请使用Net :: HTTP :: Proxy代替Net :: HTTP

For instance 例如

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

instead of 代替

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM