2010-04-08 32 views
0

考慮下面的代碼...Net :: HTTP :: Unauthorized - 我如何獲得WWW-Authenticate頭文件?

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 

克里斯

回答

2

一種選擇是使用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"'