<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ruby code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 23:57:12 GMT</pubDate>
    <description>DZone Snippets: ruby code</description>
    <item>
      <title>Custom HTTP/HTTPS GET/POST queries in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/788</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;require 'net/https'&lt;br /&gt;&lt;br /&gt;http = Net::HTTP.new('profil.wp.pl', 443)&lt;br /&gt;http.use_ssl = true&lt;br /&gt;path = '/login.html'&lt;br /&gt;&lt;br /&gt;# GET request -&gt; so the host can set his cookies&lt;br /&gt;resp, data = http.get(path, nil)&lt;br /&gt;cookie = resp.response['set-cookie']&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# POST request -&gt; logging in&lt;br /&gt;data = 'serwis=wp.pl&amp;url=profil.html&amp;tryLogin=1&amp;countTest=1&amp;logowaniessl=1&amp;login_username=blah&amp;login_password=blah'&lt;br /&gt;headers = {&lt;br /&gt;  'Cookie' =&gt; cookie,&lt;br /&gt;  'Referer' =&gt; 'http://profil.wp.pl/login.html',&lt;br /&gt;  'Content-Type' =&gt; 'application/x-www-form-urlencoded'&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;resp, data = http.post(path, data, headers)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# Output on the screen -&gt; we should get either a 302 redirect (after a successful login) or an error page&lt;br /&gt;puts 'Code = ' + resp.code&lt;br /&gt;puts 'Message = ' + resp.message&lt;br /&gt;resp.each {|key, val| puts key + ' = ' + val}&lt;br /&gt;puts data&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 06 Oct 2005 03:50:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/788</guid>
      <author>paulgoscicki (Paul Goscicki)</author>
    </item>
  </channel>
</rss>
