【问题标题】:Net::HTTP::Unauthorized - how do I get at the WWW-Authenticate header?Net::HTTP::Unauthorized - 我如何获得 WWW-Authenticate 标头?
【发布时间】:2010-04-08 10:36:04
【问题描述】:

鉴于下面的代码...

Net::HTTP.start('localhost', 4000) do |http|
    #
    #   usual stuff omitted for clarity
    #
    @response = http.request(req)
end

...如果(行为良好的)服务器返回 401(未经授权)响应,我如何获取 WWW_Authenticate 标头?

我得到的最好的解决方案一点都不好......

class Net::HTTPUnauthorized
    def get_header(h)
        _return = nil

        target = h.upcase

        self.header.each_header do |k, v|
            if k.upcase == target
                _return = v
                break
            end
        end

        _return
    end
end

克里斯

【问题讨论】:

    标签: ruby-on-rails ruby merb


    【解决方案1】:

    一种选择是使用halorgium's Rack-Client,它将Net::HTTP 与机架端点包装在一起。然后,您将与远程服务器进行交互,就好像它是一个 Rack 应用程序一样:

    response = Rack::Client.get("http://localhost:4000/foo/bar.baz")
    response.code
    # => 401
    response.headers['WWW-Authenticate']
    # => 'Basic realm="Control Panel"'
    

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 2018-10-19
      • 1970-01-01
      • 2012-10-12
      • 2013-09-04
      相关资源
      最近更新 更多