mercredi, septembre 12, 2007

Ruby net/https and invalid server certificate

Again in English since there is so few documentation. Here's the code to connect to a https server using ssl and certificate (using net/https standard lib):
def get_url(url, &block)
 puts "Getting url: #{url}"

 uri = URI.parse(url)
 http =, uri.port)
 http.use_ssl = true
 http.verify_mode = OpenSSL::SSL::VERIFY_PEER
 http.ca_file = ''
 pem_file = 'certificate-privateKey.pem'
 http.cert =
 http.key =

 http.start {
   http.request_get(uri.path) {|res|
     @page.response = res
For the pem format see my other article: Https, Ssl and Ruby
If the server ca is "strange" (I don't know in which way I'm not an ssl expert), you will get those kind of error messages:
.../http.rb:586:in `connect': certificate verify failed (OpenSSL::SSL::SSLError)
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.ca_file = ''
can be replace with
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
Technorati tags:

Aucun commentaire: