代理的 Rails 应用程序

发布于 2024-08-13 15:57:15 字数 2170 浏览 8 评论 0原文

我正在实施具有 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'

我怎样才能完成这项工作?

I'm implementing Rails alumni application with Facebook API support. One of the requirements is to post a message from the application directly to facebook wall. 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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-08-20 15:57:15

如果您的校友应用程序服务器在大学防火墙内运行,请使用 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 >通过代理访问

Use Net::HTTP::Proxy instead of Net::HTTP if your alumni application server is running within the university firewall

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文