Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 获取不带HTML的URL标题_Ruby On Rails_Http - Fatal编程技术网

Ruby on rails 获取不带HTML的URL标题

Ruby on rails 获取不带HTML的URL标题,ruby-on-rails,http,Ruby On Rails,Http,有点奇怪的问题。有没有办法让Web服务器只返回标题而不返回HTML本身 我想向服务器请求一个URL,看看它是否有效(不是404/500/etc),并遵循重定向(如果存在),但不获取实际的HTML内容 谢谢 最好是在Ruby中实现这一点 使用HEAD而不是GET或POST 第9.4节使用Ruby的net/http和Mak提到的HEAD方法。从命令行检查ri Net::HTTP#head以获取信息。根据建议,检查Net::HTTP库 require 'net/http' Net::HTTP.new

有点奇怪的问题。有没有办法让Web服务器只返回标题而不返回HTML本身

我想向服务器请求一个URL,看看它是否有效(不是404/500/etc),并遵循重定向(如果存在),但不获取实际的HTML内容

谢谢

  • 最好是在Ruby中实现这一点

使用HEAD而不是GET或POST


第9.4节使用Ruby的net/http和Mak提到的HEAD方法。从命令行检查
ri Net::HTTP#head
以获取信息。

根据建议,检查Net::HTTP库

require 'net/http'
Net::HTTP.new('www.twitter.com').request_head('/').class

这正是HEADHTTP方法所做的

对于Ruby来说,有一个漂亮的gem,它比允许执行HEAD请求的低级net/http简单得多

gem install rest-open-uri
然后

irb>需要“rubygems” =>正确 irb>需要“rest打开uri” =>正确 irb>sio=打开(“http://stackoverflow.com“,:method=>:head) => # irb>sio.meta =>{“expires”=>“Tue,2010年11月30日18:08:47 GMT”,“上次修改”=>“Tue,2010年11月30日18:07:47 GMT”,“内容类型”=>“text/html;charset=utf-8”,“date”=>“Tue,2010年11月30日18:08:27 GMT”,“内容长度”=>“193779”,“缓存控制”=>“public,max age=18”,“vary”=>“*”} irb>sio状态 =>[“200”,“确定”] 它遵循重定向。当主机不存在时,您必须为SocketError解救;如果文件不存在,则必须为OpenURI::HTTPError解救


如果你想要更有力的东西,看看或。

事实上,我不得不把潘图利斯的答案折叠成我自己的答案。似乎有两种URL,fns都不是单独工作的,所以我这样做了

module URI

  def self.online?(uri)
    URI.exists?(uri)
  end

  def self.exists?(uri)
    URI.exists_ver1?(uri)
  end

  def self.exists_ver1?(url)
    @url = url
    ["http://", "https://"].each do |prefix|
      url = url.gsub(prefix, "")
    end

    begin
      code = Net::HTTP.new(url).request_head('/').code
      [2,3].include?(code.to_i/100)
    rescue
      URI.exists_ver2?(@url)
    end
  end


  def self.exists_ver2?(url)
    url = "http://#{url}" if URI.parse(url).scheme.nil?
    return false unless URI.is_a?(url)
    uri = URI(url)
    begin
      request = Net::HTTP.new uri.host
      response= request.request_head uri.path
      #http status code 200s and 300s are ok, everything else is an error
      [2,3].include? response.code.to_i/100
    rescue
      false
    end
  end
end

我已经为此绞尽脑汁一段时间了,这是我见过的最清晰的答案
module URI

  def self.online?(uri)
    URI.exists?(uri)
  end

  def self.exists?(uri)
    URI.exists_ver1?(uri)
  end

  def self.exists_ver1?(url)
    @url = url
    ["http://", "https://"].each do |prefix|
      url = url.gsub(prefix, "")
    end

    begin
      code = Net::HTTP.new(url).request_head('/').code
      [2,3].include?(code.to_i/100)
    rescue
      URI.exists_ver2?(@url)
    end
  end


  def self.exists_ver2?(url)
    url = "http://#{url}" if URI.parse(url).scheme.nil?
    return false unless URI.is_a?(url)
    uri = URI(url)
    begin
      request = Net::HTTP.new uri.host
      response= request.request_head uri.path
      #http status code 200s and 300s are ok, everything else is an error
      [2,3].include? response.code.to_i/100
    rescue
      false
    end
  end
end