我想登錄到公司內部網站上有一個基本的身份驗證彈出對話框和基於窗體的身份驗證。這是我正在使用的代碼(這導致了401 =>淨:: HTTPUnauthorized錯誤):機械化基礎和表單身份驗證(紅寶石)
require 'rubygems'
require 'mechanize'
require 'logger'
agent = WWW::Mechanize.new { |a| a.log = Logger.new("mech.log") }
agent.user_agent_alias = 'Windows Mozilla'
agent.basic_auth('username','password')
agent.get('http://example.com') do |page|
puts page.title
end
這導致401 =>淨:: HTTPUnauthorized錯誤。下面是mech.log信息:
I, [2011-03-13T17:25:21.900631 #22128] INFO -- : Net::HTTP::Get: /index.asp?LogIn=yes&action=7
D, [2011-03-13T17:25:21.901631 #22128] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2011-03-13T17:25:21.901631 #22128] DEBUG -- : request-header: accept => */*
D, [2011-03-13T17:25:21.901631 #22128] DEBUG -- : request-header: user-agent => Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6
D, [2011-03-13T17:25:21.902631 #22128] DEBUG -- : request-header: connection => keep-alive
D, [2011-03-13T17:25:21.902631 #22128] DEBUG -- : request-header: accept-encoding => gzip,identity
D, [2011-03-13T17:25:21.902631 #22128] DEBUG -- : request-header: host => example.com
D, [2011-03-13T17:25:21.902631 #22128] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2011-03-13T17:25:21.903631 #22128] DEBUG -- : request-header: keep-alive => 300
D, [2011-03-13T17:25:22.813722 #22128] DEBUG -- : Read 24 bytes
D, [2011-03-13T17:25:22.814722 #22128] DEBUG -- : response-header: content-type => text/html
D, [2011-03-13T17:25:22.815722 #22128] DEBUG -- : response-header: connection => close
D, [2011-03-13T17:25:22.815722 #22128] DEBUG -- : response-header: www-authenticate => Negotiate, NTLM, Basic realm="example.com"
D, [2011-03-13T17:25:22.816722 #22128] DEBUG -- : response-header: date => Sun, 13 Mar 2011 11:55:21 GMT
D, [2011-03-13T17:25:22.817722 #22128] DEBUG -- : response-header: server => Microsoft-IIS/5.0
D, [2011-03-13T17:25:22.817722 #22128] DEBUG -- : response-header: content-length => 24
I, [2011-03-13T17:25:22.819723 #22128] INFO -- : status: 401
在這一點上,我只是想通過第一個基本認證。我注意到一件事,那就是服務器是IIS,並且在Mechanize documentation中有一個名爲gen_auth_headers()的公共函數,但是在我使用此函數的gem代碼中不存在,以及他在做什麼代碼超越了我。
TIA, NABS
對不起,我忘了包括那一行。實際上,我使用了basic_auth,這是auth的別名 – Nabs 2011-03-14 20:33:24
我已經使用Mechanize 1.0.0測試了上面的代碼,它可以工作。可能是你使用的任何版本的錯誤。 – 2011-03-15 14:05:51
我相信agent.auth現在已被棄用。而是使用agent.add_auth(領域,用戶名,密碼) – 2012-12-17 13:22:06