要使用Ruby进行post操作首先需要安装mechanize
(为了防止被博客转换成html代码所以每一行前面添加了’#‘)
<#body>
<
Name: <
Age: <
<
<
<#body>
<#
输入命令:gem install mechanize (在线安装)
我们首先看提交post的网页
<#html>
#
formaction="welcome.php" method="post">
#
input type="text"name="name" />
#
input type="text"name="age" />
#
input type="submit"/>
#
/form>
/html>
那么相应的ruby代码是:
require 'mechanize'
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Windows IE7'
page = agent.get("http://127.0.0.1/StuPHP/Testpost.html")
form0 = page.forms[0]
form0.fields[0].value = "shunjian"
form0.fields[1].value = 22
results = form0.submit
puts results.body