How to fetch Cookie from response

R

Ramesh Lal

Hi,
I'm trying to write a program which will try to login to my web site
by passing user & password. The site is in ASP.net for which I'm trying
to authenticate.

Somehow, I'm having problem with fetching cookies from the response &
passing it to next request.

Can you please tell me how to retrieve cookies from response in ruby?
I do not want to extract just set-cookie as I tried that & its not
working. Some how its not giving me complete cookie & hence I'm getting
back the login page itself.

Here is the code snippet
Code:
servicehost = Config::ADMIN_HOST
serviceport = Config::ADMIN_PORT
accountname = Config::ADMIN_ACCOUNT_NAME
accountpwd = Config::ADMIN_ACCOUNT_PWD
monitorid = Config::USER_AGENT
loginpage = Config::ADMIN_LOGINPAGE

http = Net::HTTP.new(servicehost, serviceport)
http.use_ssl = false
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start do |http|

request1 = Net::HTTP::Get.new(loginpage, {"User-Agent" => monitorid})
response1 = http.request(request1)
puts "1st Code = #{response1.code}"

location = response1.response['location']
puts "location=#{location}"

request1 = Net::HTTP::Get.new(location, {"User-Agent" => monitorid})
response1 = http.request(request1)

puts "2nd Code = #{response1.code}"
puts "2nd cookie= #{response1.response['set-cookie']}"

/__VIEWSTATE" value="(.*)"/.match(response1.body)
viewstate = $1

/WebForm_Do(.*)"/.match(response1.body)
eventvalidation = $1
eventvalidation = URI.escape(eventvalidation)

puts eventvalidation

req = Net::HTTP::Post.new(location, {"User-Agent" => monitorid,
'Content-Type' => 'application/x-www-form-urlencoded'})

req.set_form_data({'txtLogin_LoginPage'=>accountname,
'txtPassword_LoginPage'=>accountpwd, 'btnLogin'=>'Login',
'__EVENTTARGET'=>'', '__EVENTARGUMENT'=>'',
'__EVENTVALIDATION'=>eventvalidation, '__VIEWSTATE'=>viewstate})

response = http.request(req)

redirect = false
case response
when Net::HTTPSuccess
#puts "OK"
when Net::HTTPRedirection
redirect = true
else
inerror = true
end

puts "3rd Code = #{response.code}"
puts "Message = #{response.message}"

response.each {|key, val| printf "%s= %s\n", key, val }
resp = response.body

if redirect
request = Net::HTTP::Get.new(loginpage, {"User-Agent" =>
monitorid})

# Below line some how does not work. Can some one provide cookie
container object code?
request.add_field 'Cookie', response.response['set-cookie']

response = http.request(request)
resp = response.body
print resp
end
end
 
E

Ehsanul Hoque

You probably want to look at the Mechanize gem instead. It'll be much easie=
r than working with Net::HTTP=2C which is a pain. Also=2C it handles cookie=
s automatically. Go to this link: http://mechanize.rubyforge.org/mechanize/=
README_rdoc.html

- Ehsan
Date: Fri=2C 18 Sep 2009 04:25:04 +0900
From: (e-mail address removed)
Subject: How to fetch Cookie from response
To: (e-mail address removed)
=20
Hi=2C
I'm trying to write a program which will try to login to my web site
by passing user & password. The site is in ASP.net for which I'm trying
to authenticate.
=20
Somehow=2C I'm having problem with fetching cookies from the response = &
passing it to next request.
=20
Can you please tell me how to retrieve cookies from response in ruby?
I do not want to extract just set-cookie as I tried that & its not
working. Some how its not giving me complete cookie & hence I'm getting
back the login page itself.
=20
Here is the code snippet
Code:
servicehost =3D Config::ADMIN_HOST
serviceport =3D Config::ADMIN_PORT
accountname =3D Config::ADMIN_ACCOUNT_NAME
accountpwd =3D Config::ADMIN_ACCOUNT_PWD
monitorid =3D Config::USER_AGENT
loginpage =3D Config::ADMIN_LOGINPAGE
=20
http =3D Net::HTTP.new(servicehost=2C serviceport)
http.use_ssl =3D false
http.verify_mode =3D OpenSSL::SSL::VERIFY_NONE
http.start do |http|
=20
request1 =3D Net::HTTP::Get.new(loginpage=2C {"User-Agent" =3D> monitor= id})
response1 =3D http.request(request1)
puts "1st Code =3D #{response1.code}"
=20
location =3D response1.response['location']
puts "location=3D#{location}"
=20
request1 =3D Net::HTTP::Get.new(location=2C {"User-Agent" =3D> monitori= d})
response1 =3D http.request(request1)
=20
puts "2nd Code =3D #{response1.code}"
puts "2nd cookie=3D #{response1.response['set-cookie']}"
=20
/__VIEWSTATE" value=3D"(.*)"/.match(response1.body)
viewstate =3D $1
=20
/WebForm_Do(.*)"/.match(response1.body)
eventvalidation =3D $1
eventvalidation =3D URI.escape(eventvalidation)
=20
puts eventvalidation
=20
req =3D Net::HTTP::Post.new(location=2C {"User-Agent" =3D> monito= rid=2C
'Content-Type' =3D> 'application/x-www-form-urlencoded'})
=20
req.set_form_data({'txtLogin_LoginPage'=3D>accountname=2C
'txtPassword_LoginPage'=3D>accountpwd=2C 'btnLogin'=3D>'Login'=2C
'__EVENTTARGET'=3D>''=2C '__EVENTARGUMENT'=3D>''=2C
'__EVENTVALIDATION'=3D>eventvalidation=2C '__VIEWSTATE'=3D>viewstate})
=20
response =3D http.request(req)
=20
redirect =3D false
case response
when Net::HTTPSuccess
#puts "OK"
when Net::HTTPRedirection
redirect =3D true
else
inerror =3D true
end
=20
puts "3rd Code =3D #{response.code}"
puts "Message =3D #{response.message}"
=20
response.each {|key=2C val| printf "%s=3D %s\n"=2C key=2C val }
resp =3D response.body
=20
if redirect
request =3D Net::HTTP::Get.new(loginpage=2C {"User-Agent" =3D>
monitorid})
=20
# Below line some how does not work. Can some one provide cookie
container object code?
request.add_field 'Cookie'=2C response.response['set-cookie']
=20
response =3D http.request(request)
resp =3D response.body
print resp
end
end
=20
--=20
Posted via http://www.ruby-forum.com/.
=20
=0A=
_________________________________________________________________=0A=
Microsoft brings you a new way to search the web. Try Bing=99 now=0A=
http://www.bing.com?form=3DMFEHPG&publ=3DWLHMTAG&crea=3DTEXT_MFEHPG_Core_ta=
gline_try bing_1x1=
 
R

Ramesh Lal

Hi Ehsan,
Thanks for the reply. But would you be able to assist me using
NET/HTTP, since we are not allowed to use any other plugin.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top