机械化和NTLM身份validation

以下代码生成401 => Net :: HTTPUnauthorized错误。

从日志中:

response-header: x-powered-by => ASP.NET response-header: content-type => text/html response-header: www-authenticate => Negotiate, NTLM response-header: date => Mon, 02 Aug 2010 19:48:17 GMT response-header: server => Microsoft-IIS/6.0 response-header: content-length => 1539 status: 401 

脚本如下:

 require 'rubygems' require 'mechanize' require 'logger' agent = WWW::Mechanize.new { |a| a.log = Logger.new("mech.log") } agent.user_agent_alias = 'Windows IE 7' agent.basic_auth("username","password") page = agent.get("http://server/loginPage.asp") 

我相信401的原因是我需要使用NTLM进行身份validation,但我一直无法找到如何执行此操作的良好示例。

Mechanize 2支持NTLM身份validation:

 m = Mechanize.new m.agent.username = 'user' m.agent.password = 'password' m.agent.domain = 'addomain' 
 agent.add_auth('http://server', 'username', 'password', nil, 'domain.name') 

http://mechanize.rubyforge.org/Mechanize.html

测试:

  • Windows Server 2012 R2 + IIS 8.5
  • Ruby 1.9.3