给定下面的代码...
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克里斯
发布于 2010-04-08 20:14:47
一种选择是使用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"'https://stackoverflow.com/questions/2599126
复制相似问题