Mechanize and NTLM Authentication

孤者浪人 提交于 2019-12-30 13:23:12

问题


The following code generates a 401 => Net::HTTPUnauthorized error.

From the log:


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

The Script is as follows:


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")

I believe the reason for the 401 is that I need to be authenticating using NTLM, but I have been unable to find a good example of how to do this.


回答1:


Mechanize 2 supports NTLM auth:

m = Mechanize.new
m.agent.username = 'user'
m.agent.password = 'password'
m.agent.domain = 'addomain'



回答2:


agent.add_auth('http://server', 'username', 'password', nil, 'domain.name')

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

tested:

  • Windows Server 2012 R2 + IIS 8.5
  • Ruby 1.9.3


来源:https://stackoverflow.com/questions/3391345/mechanize-and-ntlm-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!