Created
September 16, 2010 15:33
-
-
Save MikePearce/582626 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
## So, post to the affiliatewindow login and see what happens. | |
# Get the libraries | |
require 'uri' | |
require 'net/http' | |
class TestLogin | |
attr_accessor :url | |
attr_accessor :headers | |
attr_accessor :params | |
def initialize(url, headers, params) | |
@url = url | |
@headers = headers | |
@params = params | |
end | |
#This method will make a request (POST or GET) and then query | |
#the repsonse, if it's a redirect, it'll call itself until it gets | |
#a success, then return it | |
def makeRequest(isPost = true,redirectLimit = 10 ) | |
# Throw an error if we've redirected too many times | |
raise "Redirect Limit Reached" if redirectLimit == 0 | |
# Parse the URL and create a new HTTP object | |
url = URI.parse(@url) | |
if (isPost == true) | |
puts "Posting" | |
http = Net::HTTP.new(url.host, 82) | |
# Create a new POST object | |
request = Net::HTTP::Post.new(url.path) | |
# Set the headers and form stuff | |
request.initialize_http_header(@headers) | |
request.set_form_data(@params) | |
# Reply | |
ret = http.request(request) | |
else | |
puts "Getting" | |
req = Net::HTTP::Get.new(url.path) | |
# Force port 82, otherwise it barfs as it thinks it's 443 | |
ret = Net::HTTP.start(url.host, 82) {|http| | |
http.request(req) | |
} | |
end | |
# Work out what to do with the result | |
case ret | |
when Net::HTTPRedirection then #do it again | |
puts "Redirecting to: "+ ret['location'] | |
@url = ret['location'] | |
makeRequest(false, redirectLimit -1) | |
else | |
puts "else" | |
end | |
# Something is wrong | |
rescue Errno::ECONNRESET | |
#puts url.port.to_s + "\n" | |
puts "Caught it: #{$!.backtrace.join("\n")}" | |
ret = false | |
# Finally return! | |
return ret | |
end | |
end | |
if __FILE__ == $0 | |
# New | |
bunny = TestLogin.new( | |
"https://vzadarwin.affiliatewindow.com/login", | |
{ | |
'email' => '[email protected]', | |
'password' => '***' | |
}, | |
{ | |
'Content-Type'=> 'application/x-www-form-urlencoded', | |
'User-Agent' => 'Ruby User Agent' | |
} | |
) | |
#start your engines! | |
begin | |
response = bunny.makeRequest | |
# Print! | |
response.each do |index, value| | |
print index +": "+ value +"\n" | |
end | |
rescue Exception => e | |
puts e | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment