Skip to content

Instantly share code, notes, and snippets.

@donv
Created March 24, 2012 21:24
Show Gist options
  • Select an option

  • Save donv/2188200 to your computer and use it in GitHub Desktop.

Select an option

Save donv/2188200 to your computer and use it in GitHub Desktop.
HTTPS example
RUBY_DESCRIPTION
require 'net/https'
response = nil
uri = URI("https://evening-ocean-4975.herokuapp.com/")
#uri = URI("https://docs.google.com/")
use_ssl = true
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = use_ssl
http.start do |http|
req = Net::HTTP::Get.new("/")
response = http.request(req)
resp = response.body
puts resp.inspect
response = resp
end
p response
macbeth:jruby uwe$ jirb
>> RUBY_DESCRIPTION
=> "jruby 1.6.7 (ruby-1.8.7-p357) (2012-02-22 3e82bc8) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_29) [darwin-x86_64-java]"
>> require 'net/https'
=> true
>> response = nil
=> nil
>> uri = URI("https://evening-ocean-4975.herokuapp.com/")
=> #<URI::HTTPS:0x6be54e00 URL:https://evening-ocean-4975.herokuapp.com/>
>> #uri = URI("https://docs.google.com/")
?> use_ssl = true
=> true
>>
?> http = Net::HTTP.new(uri.host, uri.port)
=> #<Net::HTTP evening-ocean-4975.herokuapp.com:443 open=false>
>> http.use_ssl = use_ssl
=> true
>>
?> http.start do |http|
?> req = Net::HTTP::Get.new("/")
>> response = http.request(req)
>> resp = response.body
>> puts resp.inspect
>> response = resp
>> end
warning: peer certificate won't be verified in this SSL session
"<html><body>You are being <a href=\"https://evening-ocean-4975.herokuapp.com/login\">redirected</a>.</body></html>"
=> "<html><body>You are being <a href="https://evening-ocean-4975.herokuapp.com/login">redirected</a>.</body></html>"
>> p response
"<html><body>You are being <a href=\"https://evening-ocean-4975.herokuapp.com/login\">redirected</a>.</body></html>"
=> nil
>>
macbeth:jruby uwe$ bin/jruby --1.8 -S jirb
irb(main):001:0> RUBY_DESCRIPTION
=> "jruby 1.7.0.dev (ruby-1.8.7-p357) (2012-03-23 dae852c) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_29) [darwin-x86_64-java]"
irb(main):002:0> require 'net/https'
=> true
irb(main):003:0> response = nil
=> nil
irb(main):004:0> uri = URI("https://evening-ocean-4975.herokuapp.com/")
=> #<URI::HTTPS:0x4e01c1f2 URL:https://evening-ocean-4975.herokuapp.com/>
irb(main):005:0> #uri = URI("https://docs.google.com/")
irb(main):006:0* use_ssl = true
=> true
irb(main):007:0>
irb(main):008:0* http = Net::HTTP.new(uri.host, uri.port)
=> #<Net::HTTP evening-ocean-4975.herokuapp.com:443 open=false>
irb(main):009:0> http.use_ssl = use_ssl
=> true
irb(main):010:0>
irb(main):011:0* http.start do |http|
irb(main):012:1* req = Net::HTTP::Get.new("/")
irb(main):013:1> response = http.request(req)
irb(main):014:1> resp = response.body
irb(main):015:1> puts resp.inspect
irb(main):016:1> response = resp
irb(main):017:1> end
warning: peer certificate won't be verified in this SSL session
"<html><body>You are being <a href=\"https://evening-ocean-4975.herokuapp.com/login\">redirected</a>.</body></html>"
=> "<html><body>You are being <a href=\"https://evening-ocean-4975.herokuapp.com/login\">redirected</a>.</body></html>"
irb(main):018:0> p response
"<html><body>You are being <a href=\"https://evening-ocean-4975.herokuapp.com/login\">redirected</a>.</body></html>"
=> nil
irb(main):019:0>
macbeth:jruby uwe$ bin/jruby -S jirb
irb(main):001:0> RUBY_DESCRIPTION
=> "jruby 1.7.0.dev (ruby-1.9.3-p139) (2012-03-23 dae852c) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_29) [darwin-x86_64-java]"
irb(main):002:0> require 'net/https'
=> true
irb(main):003:0> response = nil
=> nil
irb(main):004:0> uri = URI("https://evening-ocean-4975.herokuapp.com/")
=> #<URI::HTTPS:0x5e7c7894 URL:https://evening-ocean-4975.herokuapp.com/>
irb(main):005:0> #uri = URI("https://docs.google.com/")
irb(main):006:0* use_ssl = true
=> true
irb(main):007:0>
irb(main):008:0* http = Net::HTTP.new(uri.host, uri.port)
=> #<Net::HTTP evening-ocean-4975.herokuapp.com:443 open=false>
irb(main):009:0> http.use_ssl = use_ssl
=> true
irb(main):010:0>
irb(main):011:0* http.start do |http|
irb(main):012:1* req = Net::HTTP::Get.new("/")
irb(main):013:1> response = http.request(req)
irb(main):014:1> resp = response.body
irb(main):015:1> puts resp.inspect
irb(main):016:1> response = resp
irb(main):017:1> end
OpenSSL::SSL::SSLError: certificate verify failed
from org/jruby/ext/openssl/SSLSocket.java:180:in `connect'
from /Users/uwe/workspace/jruby/jruby/lib/ruby/1.9/net/http.rb:799:in `connect'
from org/jruby/ext/timeout/Timeout.java:103:in `timeout'
from /Users/uwe/workspace/jruby/jruby/lib/ruby/1.9/net/http.rb:799:in `connect'
from /Users/uwe/workspace/jruby/jruby/lib/ruby/1.9/net/http.rb:755:in `do_start'
from /Users/uwe/workspace/jruby/jruby/lib/ruby/1.9/net/http.rb:744:in `start'
from (irb):11:in `evaluate'
from org/jruby/RubyKernel.java:1022:in `eval'
from org/jruby/RubyKernel.java:1338:in `loop'
from org/jruby/RubyKernel.java:1131:in `catch'
from org/jruby/RubyKernel.java:1131:in `catch'
from /Users/uwe/workspace/jruby/jruby/bin/jirb:13:in `(root)'
irb(main):018:0> p response
nil
=> nil
irb(main):019:0>
@donv
Copy link
Author

donv commented Mar 24, 2012

What was needed was

http.verify_mode = OpenSSL::SSL::VERIFY_NONE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment