【问题标题】:Ruby net http request loop method if post request didn't send如果 post 请求未发送,则 Ruby net http 请求循环方法
【发布时间】:2013-10-19 00:22:38
【问题描述】:

我有这样的课程(一些代码被删除):

class Terms

  def get_connection(path, proxy_addr, proxy_port)
    @http = Net::HTTP.new('*', 443, proxy_addr, proxy_port)
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    @http.read_timeout = 500
    header = {
      'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0',
      'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
      'Accept-Language' => 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',  
      'Connection' => 'keep-alive'
    }
    @resp_get, @data_get = @http.get(path, header)
    @get_body_text = @resp_get.body
    return @resp_get
  end
  def first(path, type_id)
    ***
    @resp_post2 = @http.post(path, data, headers)
    return @resp_post2
  end
end

然后我在 main 中调用它:

v = Terms.get_connection
v.first

但主要问题是我的网站可能已关闭,并且我可能会收到错误,即无法访问 @resp_post2 = @http.post(path, data, headers)。我的问题是如何循环这个帖子请求,并检查是否有问题,然后再次尝试发送这个帖子请求。也许使用try catch,也许使用while,但是如何编写这段代码?如何检查页面是否存在以及连接是否存在?

【问题讨论】:

    标签: ruby net-http


    【解决方案1】:

    我认为您可以使用第一种方法开始救援

    def first(path, type_id)
      begin
       @resp_post2 = @http.post(path, data, headers)
       return @resp_post2
      rescue
        retry #it will retry it once more
      end
    end
    

    【讨论】:

    • 以及如何拯救相同的mwthod)?
    • 如果我收到错误,似乎连接已断开,我需要再发一次这篇文章
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2022-08-11
    • 2014-02-14
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多