From: Robert Dober Date: 2006-10-10T23:26:03+09:00 Subject: Re: Posting over https ------=_Part_10434_5862194.1160490360476 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 10/10/06, Andrew Stewart wrote: > > Hello again, > > > fortytwo :/var/www/html > ruby post.rb https://fortytwo.merseine.nu/ > > form.cgi > > --- > > key1: > > - value1 > > key2: > > - value2 > > keyn: > > - valuen > > Many thanks for the pointers to Mechanize, Hpricot and this fully- > fledged code sample. I do appreciate your taking the time to > respond, and so quickly. > > Adapting my code in the light of Ara's code above, with > 'self.set_debug_output $stderr' added to the HTTPS initialize method, > I compared the client-server conversation with curl's, which I knew > worked. This led me to discover that the server expected the content- > type header to be set to 'application/x-www-form-urlencoded'. > > For the record, here's the code which works with the server I am > talking to (Protx's payment gateway). Now that I have it working, > I'll take Aaron and Richard's advice and look into replacing it with > higher-level code using Mechanize. > > require 'net/https' > require 'uri' > > class HTTPS < Net::HTTP > def initialize *a, &b > super > self.use_ssl =3D true > self.verify_mode =3D OpenSSL::SSL::VERIFY_NONE > #self.set_debug_output $stderr > end > end > > url =3D 'https://some.host.com/foo/bar.asp?service=3Dsomeservice' > uri =3D URI.parse url > data =3D { > 'key1' =3D> 'value1', > 'key2' =3D> 'value2', > 'keyn' =3D> 'valuen' > } > e =3D lambda {|x| URI.escape x} > q =3D lambda {|h| h.map {|k,v| [e[k], e[v]].join '=3D'}.join('&')} > form =3D q[data] > HTTPS.start(uri.host, uri.port) do |https| > response =3D https.post("#{uri.path}?#{uri.query}", form, {'content- > type' =3D> 'application/x-www-form-urlencoded'}) > puts response.body > end > > Regards, > Andy Stewart > > Thanks for sharing this, I could not help you out as I lost my old POST code, now I'll just adapt your's (thx Ara) :) Robert --=20 Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_10434_5862194.1160490360476--