[OT] Does Net::HTTP support secure connections?


#1

Hi !

I’m trying to connect to an https URL, but it seems it doesn’t work.
Has anyone successfully connected from Ruby to an HTTPS server ?

Thanks !


#2

yes it does.

require ‘net/http’
require ‘net/https’

        h = Net::HTTP.new("www.google.com", 443)
        h.use_ssl = true
        resp, data = h.get("/", nil )
        @message = resp.message
        resp.code

Hope that helps.


#3

Hello Brian !

2006/3/24, Brian H. removed_email_address@domain.invalid:

require ‘net/http’
require ‘net/https’

        h = Net::HTTP.new("www.google.com", 443)
        h.use_ssl = true
        resp, data = h.get("/", nil )
        @message = resp.message
        resp.code

Works like a charm. Thanks !


#4

A bit off topic, but why use Net::HTTP if you don’t have to. open-uri
(in
the standard library) is SOOOO much simpler:

require ‘open-uri’

open(“https://www.google.com”) do |f|
@message = f.gets(nil)
f.status # array of messages but same effect
end


#5

Well, let’s see…

irb(main):030:0> require ‘open-uri’
=> false
irb(main):031:0>
irb(main):032:0* open(“https://www.google.com”) do |f|
irb(main):033:1* @message = f.gets(nil)
irb(main):034:1> f.status # array of messages but same effect
irb(main):035:1> end
OpenSSL::SSL::SSLError: certificate verify failed
from c:/ruby/lib/ruby/1.8/net/http.rb:588:in connect' from c:/ruby/lib/ruby/1.8/net/http.rb:588:inconnect’
from c:/ruby/lib/ruby/1.8/net/http.rb:555:in do_start' from c:/ruby/lib/ruby/1.8/net/http.rb:544:instart’
from c:/ruby/lib/ruby/1.8/open-uri.rb:245:in open_http' from c:/ruby/lib/ruby/1.8/open-uri.rb:629:inbuffer_open’
from c:/ruby/lib/ruby/1.8/open-uri.rb:167:in open_loop' from c:/ruby/lib/ruby/1.8/open-uri.rb:165:inopen_loop’
from c:/ruby/lib/ruby/1.8/open-uri.rb:135:in open_uri' from c:/ruby/lib/ruby/1.8/open-uri.rb:531:inopen’
from c:/ruby/lib/ruby/1.8/open-uri.rb:86:in `open’
from (irb):32

That’s why I don’t use it! :slight_smile:

Seriously, net/https warns of certificate issues but does not throw
exceptions. Not every site has valid certificates and so I tend to
always go
the somewhat longer route.


#6

Good point, good point. I probably would have seen that if OpenSSL
actually
worked on Windows w/ 1.8.4 (or at least, with my one-click installer’s
version of 1.8.4).


#7

On 3/24/06, Justin B. removed_email_address@domain.invalid wrote:

Good point, good point. I probably would have seen that if OpenSSL
actually worked on Windows w/ 1.8.4 (or at least, with my one-click
installer’s version of 1.8.4).

Have you submitted this as a bug to the instant rails team? The project
site can be found at http://rubyforge.org/projects/instantrails . I’d
highly suggest you submit any problems like this that you run into so
that
the team can get them fixed for the next release.


#8

When I try it I get:

ArgumentError: open-uri doesn’t support https.
from c:/ruby/lib/ruby/1.8/open-uri.rb:583:in proxy_open' from c:/ruby/lib/ruby/1.8/open-uri.rb:525:indirect_open’
from c:/ruby/lib/ruby/1.8/open-uri.rb:169:in open_loop' from c:/ruby/lib/ruby/1.8/open-uri.rb:164:incatch’
from c:/ruby/lib/ruby/1.8/open-uri.rb:164:in open_loop' from c:/ruby/lib/ruby/1.8/open-uri.rb:134:inopen_uri’
from c:/ruby/lib/ruby/1.8/open-uri.rb:424:in open' from c:/ruby/lib/ruby/1.8/open-uri.rb:85:inopen’
from (irb):10

But Net::HTTP worked fine.

b

(windoze w/ ruby 1.8.2 (2004-12-25) [i386-mswin32])


#9

Done (its actually the one-click installer). Thanks for the reminder.