8

I need an application to block an HTTP request so I had to add a couple of lines of code, the only piece I couldn't figure out was the statement if uri.scheme == 'https'; http.use_ssl = true is there a way I can set http/https in the current statement:

Net::HTTP.new(uri.host, uri.port).start do |http|

  # Causes and IOError... 
  if uri.scheme == 'https'
    http.use_ssl = true
  end

  request = Net::HTTP::Get.new(uri.request_uri)
  http.request(request)
end

Added: IOError: use_ssl value changed, but session already started

Jordon Bedwell
  • 3,189
  • 3
  • 23
  • 32

2 Answers2

20

Per the documentation, you cannot call use_ssl= after starting the session (i.e. after start). You have to set it before, e.g.:

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

http.start do |h|
  h.request Net::HTTP::Get.new(uri.request_uri)
end
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
5

Try to change your code to

http = Net::HTTP.new(uri.host, uri.port)

if uri.scheme =='https'
  http.use_ssl = true
end

http.start do
  request = Net::HTTP::Get.new(uri.request_uri)
  puts http.request(request)
end
Jordon Bedwell
  • 3,189
  • 3
  • 23
  • 32
WarHog
  • 8,622
  • 2
  • 29
  • 23