在使用Ruby获取正文之前,如何检查Content-type标头?

| 我需要一种能够获取网页的方式/库,如下例所示:
result = http_client.get(\'/some_page.html\') do |response|
  if response.content_type == \'text/html\' and response.code == 200
    response.read_body #the headers are returned along with page body
  else
    #not reading the body so only the headers are returned without body
  end
end
现在,如果是带有成功响应的\“ text / html \”页面:
p result.code #>200
p result.content_type #>text/html
p result.body #><DOCTYPE html...
如果是非\“ text / html \”页面或非200页面:
p result.code #>404
p result.content_type #>text/html
p result.body #>nil
所有这些都必须在向Web服务器发出的一个请求中完成。发出HTTP HEAD请求检查标头,然后发出HTTP GET请求获取正文是不可接受的,因为这将导致2个请求。 哪些宝石/图书馆提供了这种可能性? 更新资料 我找到了一个深入到net / http库的解决方案:
client.request_get(uri.request_uri) do |res|
  if res.content_type == \'text/html\'
    res.read_body
  else
    res.instance_eval {@body_exist = false}
  end
end
    
已邀请:
        我找到了一个深入到net / http库的解决方案:
client.request_get(uri.request_uri) do |res|
  if res.content_type == \'text/html\'
    res.read_body
  else
    res.instance_eval {@body_exist = false}
  end
end
    
        也许HTTP HEAD返回您想要的。 应该支持HEAD,因为此链接建议 http://rubydoc.info/gems/httpclient/2.1.5.2/HTTPClient     

要回复问题请先登录注册