是否可以修复 OpenTimeout 异常?

问题描述

我们有 2 个用 Rails 和 .Net Framework 4.8 编写的 REST Api 应用程序。我们从前端调用 rails api。对于某些服务,我们从 Rails API 项目中调用 .Net REST API。 .Net API 项目部署到测试服务器。

奇怪的是,如果我从邮递员那里使用它们,我可以获得对 .Net Rest API 端点的响应。每当我尝试从 Rails API 项目调用 .Net Rest API 端点时,我都会收到 OpenTimeout 异常。我尝试为打开超时设置不同的超时,但我不明白为什么会抛出该错误

我团队的其他人没有这样的问题。我是唯一面临这个问题的人。我确定问题来自 rails API 项目。但我找不到它是什么。

下面是我们用来发出 Http 请求的 CustomHttp 类。

require 'net/http'
require 'uri'
require 'json'

class CustomHttp
  VERB_MAP = {
    get: Net::HTTP::Get,post: Net::HTTP::Post,put: Net::HTTP::Put,delete: Net::HTTP::Delete,patch: Net::HTTP::Patch
  }.freeze

  def initialize(endpoint,token = '',client = nil,timeout = 240)
    @endpoint = endpoint
    @token = token
    @base_url = '' # .Net Web API base url
    @timeout = timeout
    @http = client.blank? ? http_client(@base_url) : client
  end

  def get(path,params)
    request_json :get,path,params
  end

  def post(path,params)
    request_json :post,params
  end

  private

  def http_client(base_url)
    use_ssl = base_url.to_s.downcase.include?('https')
    uri = URI.parse(base_url)
    http = Net::HTTP.new(uri.host,uri.port)
    http.use_ssl = use_ssl
    http.read_timeout = @timeout
    http
  end

  def request_json(method,params)
    response = request(method,params)
    body = JSON.parse(response.body,symbolize_names: true).with_indifferent_access

    {
      code: response.code,body: body,headers: response.instance_variable_get('@header')
    }.with_indifferent_access
  rescue JSON::ParserError => e
    exception_parser(e,response)
  rescue Timeout::Error => e
    exception_parser(e,response)
  rescue => e
    exception_parser(e,response)
  end

  def exception_parser(e,response)
    status_code = response.present? && response.code.to_i > 299 ? response.code.to_i : 500
    error_message = !response.respond_to?(:message) || response.message.blank? ? e.message : response.message
    {
      code: status_code,message: error_message,body: {success: false},headers: response.instance_variable_get('@header')
    }.with_indifferent_access
  end

  def encode_path_params(path,params)
    encoded = URI.encode_www_form(params)
    "#{path}?#{encoded}"
  end

  def add_authentication(request)
    req = request.clone
    req['Authorization'] = "Bearer #{@token}" unless @token.blank?
    req
  end

  def request(method,params = {})
    req = http_verb_for(method,params,path)
    final_req = add_authentication(req)
    @http.request(final_req)
  end

  def http_verb_for(method,path)
    case method
      when :get
        full_path = "#{@base_url}#{encode_path_params(path,params)}"
        req = VERB_MAP[method.to_sym].new(full_path)
      when :post_json
        full_path = "#{@base_url}#{path}"
        req = VERB_MAP[:post].new(full_path,'Content-Type' => 'application/json')
        req.body = params.to_json
      else
        full_path = "#{@base_url}#{path}"
        req = VERB_MAP[method.to_sym].new(full_path)
        req.set_form_data(params)
    end
    req
  end
end

但是,下面的脚本有效。

require 'net/http'

AUTH_TOKEN = '' # place your auth token here

uri = URI.parse("Your endpoint to connect")
header = {'Content-Type': 'application/json','Authorization': "Bearer #{AUTH_TOKEN}"}

http = Net::HTTP.new(uri.host,uri.port)
request = Net::HTTP::Get.new(uri.request_uri,header)
http.open_timeout = 10
response = http.request(request)
puts response.body

不知道为什么我无法从 rails API 项目连接到服务器。

我们在 macOS rails 5.0.7 中使用 ruby-2.5.3Catalina。 如果我能解决这个问题会很有帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...