From: Ehsanul Hoque Date: 2009-09-18T04:46:48+09:00 Subject: Re: How to fetch Cookie from response --_165a2b1e-9c1d-4bc3-8be9-2b460a129089_ Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable 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: freephoneid@yahoo.com > Subject: How to fetch Cookie from response > To: ruby-talk@ruby-lang.org >=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 > [/code] > --=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= --_165a2b1e-9c1d-4bc3-8be9-2b460a129089_--