【问题标题】:Get only response headers in em_http_request仅获取 em_http_request 中的响应标头
【发布时间】:2014-10-24 15:22:00
【问题描述】:

如何在 em_http_request 中仅获取响应标头?

我尝试使用此代码:

EventMachine.run do
  http = EventMachine::HttpRequest.new('my_url').get
  http.headers do |headers|
    Fiber.current.resume headers
  end
end

但我不想接受整个身体。如何停止请求的执行? http.close 不起作用。

UPD
http.instance_variable_get(:'@conn').close 帮助我,但也许你知道更有趣的解决方案

【问题讨论】:

    标签: ruby eventmachine em-http-request


    【解决方案1】:

    如果你不想要尸体,you should do a HEAD request instead of a GET。要终止事件循环,您需要显式调用EventMachine.stop

    EventMachine.run do
      http = EventMachine::HttpRequest.new('my_url').head
      http.headers do |headers|
        # do something with headers
    
        EventMachine.stop
      end
    end
    

    【讨论】:

    • 不是事件循环。只是请求。见 UPD
    • +1,是的,HEAD 请求是执行此操作的正确方法。
    • 当您发出HEAD 请求时,根据定义收到标头后请求结束。我想在调用header 回调时,连接可能已经关闭。如果您不想终止事件循环,只需删除 EventMachine.stop
    猜你喜欢
    • 2014-03-10
    • 2012-04-21
    • 2015-11-15
    • 2016-11-16
    • 2017-11-06
    • 2019-09-07
    • 2012-05-02
    • 1970-01-01
    相关资源
    最近更新 更多