我需要一个阻止HTTP请求的应用程序,所以我不得不添加几行代码,如果uri.scheme ==’https’,那么唯一我想不到的就是语句; http.use_ssl = true有没有办法在当前语句中设置http / https:
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
补充:IOError:use_ssl值已更改,但会话已启动
解决方法
按照
the documentation,您不能在启动会话后(即启动后)调用use_ssl =.你必须先设置它,例如:
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