发出http请求时出错。超时::错误和IOError:use_ssl值已更改,但会话已启动

时间:2013-10-27 20:22:12

标签: ruby-on-rails ruby

我有一个奇怪的问题,我有一种感觉只是一个很小的明显错误。

当我进行以下实施时;

url = URI.parse(helper.full_url)
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) { |http|
  http.request(req)
}

给我下面的错误;

Timeout :: Error

但是这个实现;

url = URI.parse(helper.full_url)
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) { |http|
  http.use_ssl = true
  http.request(req)
}

给了我这个错误。

IOError: use_ssl value changed, but session already started

可能是什么问题?

1 个答案:

答案 0 :(得分:3)

你不能在Net :: HTTP.start之后使用http.use_ssl,你以前应该使用它。看http://www.ruby-doc.org/stdlib-1.9.2/libdoc/net/http/rdoc/Net/HTTP.html#method-i-use_ssl-3D

# File net/http.rb, line 591
def use_ssl=(flag)
  flag = (flag ? true : false)
  if started? and @use_ssl != flag
    raise IOError, "use_ssl value changed, but session already started"
  end
  @use_ssl = flag
end

试试这个:

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

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