在 Ruby 中获取 URL 的重定向 [英] Get redirect of a URL in Ruby

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

问题描述

根据 Facebook 图形 API,我们可以使用此(示例)请求用户个人资料图片:

According to Facebook graph API we can request a user profile picture with this (example):

https://graph.facebook.com/1489686594/picture

但是上一个链接的真实图片网址是:

But the real image URL of the previous link is:

http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs356.snc4/41721_1489686594_527_q.jpg

如果您在浏览器上输入第一个链接,它会将您重定向到第二个链接.

If you type the first link on your browser, it will redirect you to the second link.

有没有办法通过 Ruby/Rails 获得完整的 URL(第二个链接),只知道第一个 URL?

Is there any way to get the full URL (second link) with Ruby/Rails, by only knowing the first URL?

(这是这个问题的重复,但对于Ruby)

(This is a repeat of this question, but for Ruby)

推荐答案

您可以使用 Net::Http 并从响应中读取 Location: 标头

You can use Net::Http and read the Location: header from the response

require 'net/http'
require 'uri'

url = URI.parse('http://www.example.com/index.html')
res = Net::HTTP.start(url.host, url.port) {|http|
  http.get('/index.html')
}
res['location']

这篇关于在 Ruby 中获取 URL 的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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