问题
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