I found really few documentation / pages in google about this subject.

So you want to get/post to an https url using ssl, certificates, pem and ruby ?

Here's what you need to do / know:

First let me say that there a re many ways to do that but find the right one is tricky!!

Get the server ca cert

First you need a ca cert the thing that firefox/Ie ask you to validate when you go to a secure website.

For XP:

"I'm using Windows XP, so all I had to do was go into Control Panel -> Internet Options -> Content tab -> Certificates -> Trusted Root Certificates -> select "Equifax Secure Certificate Authority" -> Export -> select Base 64 Encoding -> save to file (EquifaxSecureCertificateAuthority.cer) " (source: Greg Houston blog)

For Firefox:

Tools > Options > Advanced > Encryption tab > View Certificates > Authorithies tab (but I don't know how to export it)

Call it "ca.secure.webapp.domain.com.crt" or change the ruby code to match your name.

Get your own certificate right

You need a certificate-privateKey.pem file to connect to the secure web site. It should look like this:


Bag attributes
blabla

Key Attributes
blabla

---begin rsa private key---
blabla
---end rsa private key-----

--begin certificate--------
blabla
--end certificate----------

If you have something else a .p12 for example, you can transform it that way:

openssl pkcs12 -in <pfx-file> -nodes -out <pem-key-file>

If it something else you need to find out how to transform it, take a look at those links for a start:

Install Httpclient

gem install httpclient

Ruby code

require 'httpclient'

client = HTTPClient.new
client.ssl_config.set_trust_ca('ca.
secure.webapp.domain.com.crt')
# set_client_cert_file(cert_file, key_file)
client.ssl_config.set_client_cert_file('certificate-privateKey.pem', 'certificate-privateKey.pem')

url = "https://secure.webapp.domain.com/something?param=value"
resp = client.get(url)
puts resp.content
puts resp.status

Links

HttpClient RDoc

lassHTTPClient
In: lib/httpclient.rb
Parent: Object

DESCRIPTION

 HTTPClient -- Client to retrieve web resources via HTTP.

How to create your client.

 1. Create simple client.
clnt = HTTPClient.new

2. Accessing resources through HTTP proxy.
clnt = HTTPClient.new("http://myproxy:8080")

3. Set User-Agent and From in HTTP request header.(nil means "No proxy")
clnt = HTTPClient.new(nil, "MyAgent", "nahi@keynauts.com")

How to retrieve web resources.

 1. Get content of specified URL.
puts clnt.get_content("http://www.ruby-lang.org/en/")

2. Do HEAD request.
res = clnt.head(uri)

3. Do GET request with query.
res = clnt.get(uri)

4. Do POST request.
res = clnt.post(uri)
res = clnt.get|post|head(uri, proxy)