【问题标题】:Net::HTTP::Get timeout issueNet::HTTP::Get 超时问题
【发布时间】:2015-11-06 22:32:01
【问题描述】:

尝试调整一小段代码表单索引控制器操作。
但是得到 Net::ReadTimeout 错误。

class PaymentMethodsController < ApplicationController
  def index
    uri = URI.parse("http://localhost:3000")
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Get.new("/api/user/profile/payment", {'Content-Type' =>'application/json'})
    request.set_form_data({token: 'vjuri@mrd.com:CFTKGNZKXC'})
    res = http.request(request)

    render text: res.body
  end
end

从 rails 控制台它就像一个魅力。

2.1.2 :027 > uri = URI.parse("http://localhost:3000") => #<URI::HTTP:0x00000004d0eb80 URL:http://localhost:3000> 
2.1.2 :028 > http = Net::HTTP.new(uri.host, uri.port) => #<Net::HTTP localhost:3000 open=false> 
2.1.2 :029 > request = Net::HTTP::Get.new("/api/user/profile/payment", {'Content-Type' =>'application/json'})
 => #<Net::HTTP::Get GET> 
2.1.2 :031 >  request.set_form_data({token: 'vjuri@mrd.com:CFTKGNZKXC'})
 => "application/x-www-form-urlencoded" 
2.1.2 :032 > res = http.request(request)
 => #<Net::HTTPOK 200 OK readbody=true> 
2.1.2 :033 > res.body
 => "{\"result\":{\"errorcode\":0,\"errormessage\":\"\",\"payments\":[]}}" 

如何从控制器强制 Net::HTTP 工作?

【问题讨论】:

    标签: ruby-on-rails ruby api


    【解决方案1】:

    这是我的问题上的answer
    原因是

    它不起作用,因为您没有使用多线程服务器。 您的请求正在进入并阻止服务器,直到它完成。 在此期间,您正在向您的 localhost 发出一个请求,该请求不是 正在处理中。

    【讨论】:

      猜你喜欢
      • 2014-11-23
      • 2017-02-18
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 2011-06-12
      • 1970-01-01
      相关资源
      最近更新 更多