在Ruby中使用Net::Http编写autologin

在Ruby中使用Net::Http编写autologin,ruby,Ruby,仔细阅读文档后,我正在为程序的autologin功能编写以下代码片段: url = URI.parse('http://localhost/login.aspx') req = Net::HTTP::Post.new(url.path) req.basic_auth 'username' 目标页面只要求正确的用户名,登录不需要密码,基本认证方法需要两个参数,用户名和密码,如果我漏掉一个,我会得到错误,我试图这样写:req.basic\u auth'username',但我仍然无法登录 谁能给我

仔细阅读文档后,我正在为程序的autologin功能编写以下代码片段:

url = URI.parse('http://localhost/login.aspx')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'username'
目标页面只要求正确的用户名,登录不需要密码,基本认证方法需要两个参数,用户名和密码,如果我漏掉一个,我会得到错误,我试图这样写:req.basic\u auth'username',但我仍然无法登录

谁能给我一个提示吗

更多信息: 我还尝试了req.basic_auth'username',它似乎不起作用,我知道这一点,因为在这一行之后还有另一行,基本上是自动提交表单。 x=Net::HTTP.post\u formURI。parsehttp://localhost/NewTask.aspx,params 把x.body

并且返回结果返回到登录页面体。

可以考虑使用。官方网站的登录示例将非常简单,对于此示例,您不需要执行代理证书和私钥操作:

require 'rubygems'
require 'mechanize'

# create Mechanize instance
agent = Mechanize.new

# set the path of the certificate file
agent.cert = 'example.cer'

# set the path of the private key file
agent.key = 'example.key'

# get the login form & fill it out with the username/password
login_form = agent.get("http://example.com/login_page").form('Login')
login_form.Userid = 'TestUser'
login_form.Password = 'TestPassword'

# submit login form
agent.submit(login_form, login_form.buttons.first)

req.basic_auth“username”应该可以工作。如果您显示应用程序的相关服务器日志,或者在运行代码时ruby抛出任何错误,可能会有所帮助?谢谢您的回复。目标站点不是本地托管的,我无法捕获服务器日志,我尝试了req.basic_auth'username',但它似乎不起作用,我知道这是因为,在这之后有一个自动提交代码,x=Net::HTTP.post_formURI.parse,params put x.body,put结果返回并重定向到登录页面。非常感谢c2h2,在挖掘了一些mechanize文档之后,我现在可以登录该网站了,看起来我也将使用mechanize重写自动表单提交部分。别担心,如果你认为正确,请标记正确的答案并投票,稍后人们遇到相同的问题时可以参考你的选择。