获取HTTP重定向页面的状态代码 [英] getting the status code of a HTTP redirected page

查看:118
本文介绍了获取HTTP重定向页面的状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用遏制来测试Ruby中的某些URL:

 需要'遏制'

def test_url()
c = Curl :: Easy.new( http://www.wikipedia.org/wiki/URL_redirection)做|卷曲|
curl.follow_location = true
curl.head = true
结束

c.perform
放置 status => + c.status
放置 body => + c.body_str
放置最终URL => + c.last_effective_url
结束

test_url

此输出:

 状态=> 301已永久移动
正文=>
最终网址=> 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



遏制库中的等效项是什么?

解决方案

找到了它。



我克隆了路缘源并进行了以下操作:

  last_effective_url 

在下面的函数中,它相当于响应代码,位于curb_easy.c,第2435行。 / p>

请注意,使用源Luke!



更新:



答案是 response_code
就我而言,代码看起来像这样:

  c = Curl :: Easy.new(HOST_NAME)做| curl | 
curl.follow_location = true
curl.head = true
end

c.perform
puts url + => + c.response_code .to_s


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?

解决方案

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

这篇关于获取HTTP重定向页面的状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆