【问题标题】:getting the status code of a HTTP redirected page获取 HTTP 重定向页面的状态码
【发布时间】:2013-04-08 11:29:45
【问题描述】:

我正在使用 Ruby 测试一些 URL:

require 'curb'

def test_url()
  c = Curl::Easy.new("http://www.wikipedia.org/wiki/URL_redirection") do |curl|
    curl.follow_location= true
    curl.head = true
  end

  c.perform
  puts "status => " + c.status
  puts "body => " + c.body_str
  puts "final url => " + c.last_effective_url
end

test_url

这个输出:

status => 301 Moved Permanently
body => 
final url => http://en.wikipedia.org/wiki/URL_redirection

在这种情况下,www.wikipedia.org/wiki/URL_redirection 重定向到 en.wikipedia.org/wiki/URL_redirection。

如您所见,我获得了 301 状态。如何获取最终响应代码的状态?

在这种情况下,它是 200,因为找到了文档。我检查了 libcurl 文档,发现了一个标志 CURLINFO_RESPONSE_CODE。

路边库中的等价物是什么?

【问题讨论】:

    标签: ruby curl curb


    【解决方案1】:

    找到了。

    我克隆了路边源并为:

    last_effective_url
    

    在下面的函数中,它是响应代码的等效项,在 2435 行的 curl_easy.c 中。

    提醒自己,“使用源卢克”!

    更新:

    答案是response_code 就我而言,代码如下所示:

    c = Curl::Easy.new(HOST_NAME) do |curl|
        curl.follow_location = true
        curl.head = true
      end
    
      c.perform
      puts url + " => " + c.response_code.to_s
    

    【讨论】:

    • 请将您修改后的代码作为您的答案。这样以后就不用再回头看源代码了。
    • 我需要等待 2 天才能允许这样做
    • 我知道,我说的是你在代码中所做的修复,在你的答案中写下来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2012-01-09
    • 2011-08-31
    相关资源
    最近更新 更多