Forum: Ruby on Rails [OT] Does Net::HTTP support secure connections ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
François B. (Guest)
on 2006-03-24 19:10
(Received via mailing list)
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 !
Brian H. (Guest)
on 2006-03-24 19:21
(Received via mailing list)
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.
François B. (Guest)
on 2006-03-24 21:17
(Received via mailing list)
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 !
Justin B. (Guest)
on 2006-03-24 23:14
(Received via mailing list)
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
Brian H. (Guest)
on 2006-03-24 23:27
(Received via mailing list)
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:in `connect'
        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:in `start'
        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:in `buffer_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:in `open_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:in `open'
        from c:/ruby/lib/ruby/1.8/open-uri.rb:86:in `open'
        from (irb):32

That's why I don't use it! :)

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.
Justin B. (Guest)
on 2006-03-24 23:34
(Received via mailing list)
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).
Tanner B. (Guest)
on 2006-03-25 00:44
(Received via mailing list)
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.
Justin B. (Guest)
on 2006-03-25 01:24
(Received via mailing list)
Done (its actually the one-click installer). Thanks for the reminder.
Ben M. (Guest)
on 2006-03-25 08:59
(Received via mailing list)
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:in `direct_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:in `catch'
         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:in `open_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:in `open'
         from (irb):10

But Net::HTTP worked fine.

b

(windoze w/ ruby 1.8.2 (2004-12-25) [i386-mswin32])
This topic is locked and can not be replied to.