Created
July 2, 2011 21:01
-
-
Save anonymous/1061645 to your computer and use it in GitHub Desktop.
This is an example of using Mechanize 2.0 and posting a form.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'mechanize' | |
require 'net/https' | |
agent = Mechanize.new | |
agent.user_agent_alias = 'Mac Safari' | |
agent.read_timeout = 60 | |
agent.follow_meta_refresh=true | |
page = agent.get("https://the.website.com/login.do") | |
current_form=page.forms.first | |
current_form.username = "myusername" | |
current_form.password = "mypassword" | |
newpage = agent.submit current_form | |
newreq = Mechanize.new | |
newreq.cookie_jar.add(URI.parse("https://the.website.com/"),agent.cookies[0]) | |
newreq.cookie_jar.add(URI.parse("https://the.website.com/"),agent.cookies[1]) | |
hash = { | |
"individualIds" => "", | |
"groupIds" => "", | |
"publicGroupIds" => "", | |
"aliasIds" => "", | |
"phoneNumbers" => "9074339549", | |
"foreignGroupNames" => "", | |
"message.parentMsgId" => "", | |
"to" => "", | |
"message.subject" => "test subject", | |
"message.text" => "test message" | |
} | |
headers = {"X-Requested-With" => "XMLHttpRequest", "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"} | |
lastpage = newreq.post("https://the.website.com/pri/messages/do-compose.do?format=json", hash ,headers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment