DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Custom HTTP/HTTPS GET/POST Queries In Ruby
First, the scripts sends the GET query to read the website cookies (for session, etc.), and then it sends a POST query with the received cookies and custom POST parameters.
require 'net/http'
require 'net/https'
http = Net::HTTP.new('profil.wp.pl', 443)
http.use_ssl = true
path = '/login.html'
# GET request -> so the host can set his cookies
resp, data = http.get(path, nil)
cookie = resp.response['set-cookie']
# POST request -> logging in
data = 'serwis=wp.pl&url=profil.html&tryLogin=1&countTest=1&logowaniessl=1&login_username=blah&login_password=blah'
headers = {
'Cookie' => cookie,
'Referer' => 'http://profil.wp.pl/login.html',
'Content-Type' => 'application/x-www-form-urlencoded'
}
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






Comments
Carla Brian replied on Sun, 2012/07/22 - 12:03am
Simab Si replied on Sun, 2012/05/20 - 7:35am
Nice to see the quality information presented in an easy and understanding manner. This is very nice to see this blog and it's really informative for the readers.
Thank you.
UK dissertations
Snippets Manager replied on Sat, 2009/06/06 - 8:53pm
Snippets Manager replied on Mon, 2006/02/20 - 11:36am
Snippets Manager replied on Mon, 2009/02/02 - 1:45pm
Snippets Manager replied on Wed, 2008/03/19 - 5:38pm
Snippets Manager replied on Thu, 2008/11/13 - 1:33am
Snippets Manager replied on Mon, 2006/02/20 - 11:36am
require "net/http" require "net/https" require "erb" require "singleton" PANEL = 'panel.dreamhost.com' PATH = '/index.cgi' USERAGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1' class Panel include Singleton def Panel.login(webid, password) unless @headers.nil? raise ScriptError, "You should already be logged in!" end @http = Net::HTTP.new(PANEL, 443) @http.use_ssl = true # GET request -> so the host can set cookies resp, data = @http.get2(PATH, {'User-Agent' => USERAGENT}) cookie = resp.response['set-cookie'].split('; ')[0] # POST request -> logging in data = "Nscmd=Nlogin&username=#{ERB::Util.url_encode(webid)}&password=#{ERB::Util.url_encode(password)}&submit=Log%20In" @headers = { 'Cookie' => cookie, 'Referer' => 'https://'+PANEL+PATH, 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => USERAGENT } puts "headers = " + @headers.inspect resp, data = @http.post2(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 end endIf you curious, I'm scraping my own API for DreamHost's Control Panel, so I don't have to labourously open my web browser every time I need to add or change something. (Yeah, I'm that lazy.) Anyway, its unfinished, I'll be refactoring the code next - the Panel.login method was just for testing.