下面给出...

的代码
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

克里斯

有帮助吗?

解决方案

一种选择是使用 halorgium的机架客户,该包装Net::HTTP与齿条端点。那么您需要与远程服务器,就好像是一个机架应用程序进行交互:

response = Rack::Client.get("http://localhost:4000/foo/bar.baz")
response.code
# => 401
response.headers['WWW-Authenticate']
# => 'Basic realm="Control Panel"'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top