0

鉴于下面的代码...

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

克里斯

4

1 回答 1

2

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

response = Rack::Client.get("http://localhost:4000/foo/bar.baz")
response.code
# => 401
response.headers['WWW-Authenticate']
# => 'Basic realm="Control Panel"'
于 2010-04-08T12:14:46.747 回答