I want to programatically download the contents of a web page, but that page is generated as the result of a POST and I cant seem to get it working.
This is the page: http://jp.translink.com.au/mobile/Input.aspx
You can enter the following values to see how it works:
From: Coorparoo Railway Station
To: Central Railway Station
I have monitored the traffic with tcpdump and have recreated it using code to the best of my ability. Here is the test code:
http = Net::HTTP.new("jp.translink.com.au", 80)
path = "/mobile/Input.aspx"
# GET request -> so the host can set his cookies
resp, data = http.get(path, nil)
cookie = resp.response['set-cookie']
viewstate = data.match(/"__VIEWSTATE" value="([^"]+)"/)[1]
# POST request -> logging in
data = "__VIEWSTATE=#{viewstate}&FromTextBox=mitchelton+railway+station&FromModeList=stopLandmark&ToTextBox=morayfield+railway+station&ToModeList=stopLandmark&VehicleList%3A1=on&HourList=11&MinuteList=40&NoonList=PM&DateList=0&goButton=Go%21"
headers = {
'Cookie' => cookie,
'Referer' => 'http://jp.translink.com.au/mobile/Input.aspx',
'origin' => 'http://jp.translink.com.au',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/530.19.2 (KHTML, like Gecko) Version/4.0.2 Safari/530.19',
'Accept' => 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
'Accept-Language' => 'en-us',
'Accept-Encoding' => 'gzip, deflate'
}
resp, data = http.post(path, data, headers)
# Output on the screen -> we should get either a 302 redirect (after a successful login) or an error page
puts 'Code = ' + resp.code
puts 'Message = ' + resp.message
resp.each {|key, val| puts key + ' = ' + val}
puts data
I get response telling me to redirect to an error page. Does anyone know how to do this successfully?
EDIT: Thank you few that responded to my question. I below is the solution to my problem :)
require 'mechanize'
agent = WWW::Mechanize.new
initial_page = agent.get('http://jp.translink.com.au/mobile/Input.aspx')
initial_form = initial_page.form('InputForm')
initial_form.FromTextBox = 'Mitchelton Railway Station'
initial_form.radiobuttons_with(:name => 'FromModeList')[1].check
initial_form.ToTextBox = 'Morayfield Railway Station'
initial_form.radiobuttons_with(:name => 'ToModeList')[1].check
initial_form.checkbox_with(:name => 'VehicleList:0').uncheck
initial_form.checkbox_with(:name => 'VehicleList:2').uncheck
go_button = initial_form.buttons[0]
result_page = agent.submit(initial_form, go_button)
puts result_page.body