使用 Mechanize (Ruby) 进行基本身份验证和表单身份验证

2024-06-19

我正在尝试登录公司内部网上的一个站点,该站点具有基本身份验证弹出对话框和基于表单的身份验证。这是我正在使用的代码(导致 401 => Net::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 => Net::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 并且在机械化文档 http://mechanize.rubyforge.org/mechanize/Mechanize/Chain/AuthHeaders.html有一个名为 gen_auth_headers() 的公共函数,但是在我使用的 gem 代码中不存在这个函数,而且他在代码中所做的事情超出了我的范围。

TIA, 纳布斯


auth该方法将在机械化版本 3 中删除,因此必须替换为add_auth http://mechanize.rubyforge.org/Mechanize.html#method-i-add_auth并提供这些凭据适用的 URI。

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
agent.user_agent_alias = 'Windows Mozilla'
agent.add_auth('http://example.com', 'username', 'password')
agent.get('http://example.com') do |page|
  puts page.title
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Mechanize (Ruby) 进行基本身份验证和表单身份验证 的相关文章

  • 无主键的 ActiveRecord 模型

    我有一个 ActiveRecord 模型GPA没有主键 class GPA lt ActiveRecord Base end 当我尝试打电话时GPA first to json I get TypeError false is not a
  • 在虾中使用列表

    我使用 prawn 创建 pdf 文件 其中包含大量表格格式的数据和一些列表 列表的问题在于 我只是使用文本作为列表 因为没有与 ul gt li 列表等效的语义 就像我在 webfrointend 中使用它们一样 所以这些清单是不合理的
  • 安装 sqlite3 公共密钥环时出错,未找到

    怎么了 我该如何安装它 PS C Users luism gt gem install sqlite3 暂时增强 MSYS MINGW 的 PATH 安装所需的 msys2 软件包 mingw w64 x86 64 sqlite3 警告 未
  • 当我运行 rake:db migrate 命令时,出现错误“未初始化常量 CreateArticles”

    我创建了一个模型 ruby 脚本 生成模型文章 简单就够了 这是迁移文件create articles rb def self up create table articles do t t column user id integer t
  • Rails 如何确定传入请求格式?

    我只是想知道 Rails 如何知道请求的格式以正确输入著名的 respond to do format format html format xml format json end 作为一个例子 考虑一下我遇到的这种情况 假设通过 java
  • 使用rails-Ajax 调用控制器方法?

    我正在尝试从视图中的按钮执行 application controller rb 中的 Ruby 方法 在昨天的一篇文章中 有人告诉我使用 Ajax 调用来执行此操作 因为如果没有它 只会在页面加载时运行 我对此很陌生 很难理解它 我安装了
  • Rails cron 每当捆绑:命令未找到

    我尝试使用 每当 每天执行一次耙子任务 我收到这个错误 bin bash bundle command not found home app rvm rubies ruby 1 9 2 p180 lib ruby site ruby 1 9
  • ruby 无法复制 Fixnum

    我有一些这样的代码 ssh files id rsa pub id rsa ssh files each with index do item index ssh files index generate ssh path creator
  • Friendly_id 和真实ID

    有什么办法可以得到真正的id使用Friendly id 修改的模型中的列 出于性能原因 我不想对其进行另一个数据库查询 广义上来说Friendly id修改to param and find方法 接下来应该可以工作 affiche Affi
  • 配置 session_store.rb 来处理登台和生产?

    我的 Rails 3 1rc6 应用程序上有一个使用子域的暂存和生产环境 我为这些环境购买并配置了不同的域名 因为默认的 some something herokuapp com 不能很好地与子域配合 当我将 session store r
  • Rails 3 - 使用 Ajax 和 jquery 更新 div 内容(嵌套资源)

    我有两个简单的模型 Pin 和 Comment Comments 属于 Pin class Pin lt ActiveRecord Base has many comments dependent destroy and class Com
  • 姜戈。登录表单的错误消息

    我制作登录名 密码表格 model class LoginForm forms Form username forms CharField max length 100 password forms CharField widget for
  • 将对象数组转换为 ActiveRecord::Relation

    我有一个对象数组 我们称其为Indicator 我想运行 Indicator 类方法 def self subjects该数组上的种类 范围等 我知道在一组对象上运行类方法的唯一方法是让它们成为 ActiveRecord Relation
  • jruby-openssl 中已初始化常量

    当在 Torquebox 上运行我的 Rails 应用程序时 我得到了很多 already initialized constant 警告源自gems jruby openssl 0 8 2 lib shared jruby openssl
  • before_filter set_locale 除了控制器

    My routes rb MyApp Application routes draw do scope locale do all resources here end namespace blog do resources posts o
  • 如何在没有 sudo 的情况下安装 gem

    在我所有的 gem 安装中 我必须执行 sudo 所以 sudo gem 安装rails 会起作用 而只有 宝石安装导轨 不管用 我该如何补救 我安装了rvm murtaza murtaza dev which rvm home murta
  • 如何在 ruby​​ on Rails 中的 haml 内的 js 设置会话变量?

    我通过 js 有表行着色 针对行组 我想让它通过会话变量记住阴影 我正在使用的 haml 部分有 Group Shading a href gt id gt row colors on On a href gt id gt row colo
  • Rails 3 > 在 rake 任务中渲染视图

    我遇到了一项需要为 Mailchimp 准备新闻通讯的 rake 任务 使用 google 搜索到的 Rails 2 x 内容 我现在有了以下代码 desc Sends newsletter to Mailchimp list task s
  • 捆绑安装无法安装 libxml-ruby

    我是 Ruby 世界的新手 我需要克隆一个现有项目 说明是 source env development bundle install 但它失败了 Using libv8 3 16 14 15 Fetching libxml ruby 2
  • 如何向 ActiveRecord 集合添加方法?

    我想为特定模型的所有集合添加一个方法 假设我想添加方法my complicated averaging method到 WeatherData 集合 WeatherData all limit 3 my complicated averag

随机推荐