Using Net::HTTP.get for an https url

前端 未结 4 2015
囚心锁ツ
囚心锁ツ 2020-11-27 14:39

I\'m trying to use Net::HTTP.get() for an https URL:

@data = Net::HTTP.get(uri, Net::HTTP.https_default_port())

However, I get

4条回答
  •  情话喂你
    2020-11-27 15:29

    EDIT: My approach works, but @jason-yeo's approach is far easier.

    It appears as of 2.1.2 the preferred a documented method is as follows (directly quoting the documentation):

    HTTPS is enabled for an HTTP connection by #use_ssl=.

    uri = URI('https://secure.example.com/some_path?query=string')
    
    Net::HTTP.start(uri.host, uri.port,   
      :use_ssl => uri.scheme == 'https') do |http|
      request = Net::HTTP::Get.new uri
    
      response = http.request request # Net::HTTPResponse object 
    end 
    

    In previous versions of Ruby you would need to require ‘net/https’ to use HTTPS. This is no longer true.

提交回复
热议问题