ruby实现rails页面login实例

做了一个ruby登录rails页面的脚本
用户名:a
密码:
require 'net/http'
require "open-uri"  
require 'rexml/document'

uri = 'http://localhost:3000/depot/login' 
url = URI.parse(uri)
html_response = nil  
open(uri) do |http|   
  html_response = http.read   
end  
#puts html_response  
form = {}

params = {} 

doc=REXML::Document.new(html_response)

token=doc.get_elements('//input[@name="authenticity_token"]').first.attribute('value').value

form['authenticity_token'] = token

#用户名
form['name'] = 'a'

#密码
form['password'] = ''

#提交登录
form['commit'] = 'Login'

post=Net::HTTP::Post.new(uri)
post.form_data=form
#p form
Net::HTTP.start(url.host, url.port) do |http|
  response = http.request(post)
  #puts response.body
end

登录成功后,将跳转到http://localhost:3000/depot,即登录成功页面。

你可能感兴趣的:(html,.net,脚本,Ruby,Rails)