2010-04-08 4 views
0

Vu le code ci-dessous ...Net :: HTTP :: Unauthorized - Comment obtenir l'en-tête WWW-Authenticate?

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

... si un serveur (bien élevé) renvoie une réponse 401 (non autorisée), comment puis-je l'en-tête de WWW_Authenticate?

La meilleure solution que j'ai est pas du tout bon ...

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 

Chris

Répondre

2

Une option serait d'utiliser halorgium's Rack-Client, qui enveloppe Net::HTTP avec un point de terminaison de rack. Vous pouvez ensuite interagir avec le serveur distant comme si elle était une application Rack:

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