getting the status code of a HTTP redirected page

♀尐吖头ヾ 提交于 2019-12-08 07:09:06

问题


I'm using curb to test some URLs in Ruby:

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

This outputs:

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

In this case, www.wikipedia.org/wiki/URL_redirection redirects to en.wikipedia.org/wiki/URL_redirection.

As you can see, I am getting a 301 status. How can I get the status of the final response code?

In this case, it is 200 because the document is found. I checked the libcurl documentation and found a flag CURLINFO_RESPONSE_CODE.

What is the equivalent in the curb library?


回答1:


Found it.

I cloned the curb source and grepped for :

last_effective_url

In the function below it was the equivalent for the response code, in curb_easy.c, line 2435.

Note to self, "Use the source Luke"!

UPDATE:

The answer is response_code In my case the code looks like so:

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


来源:https://stackoverflow.com/questions/15877906/getting-the-status-code-of-a-http-redirected-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!