在 Ruby 中获取 URL 的重定向

编程入门 行业动态 更新时间:2024-10-27 04:28:18
本文介绍了在 Ruby 中获取 URL 的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

graph.facebook/1489686594/picture

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

But the real image URL of the previous link is:

profile.ak.fbcdn/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('www.example/index.html') res = Net::HTTP.start(url.host, url.port) {|http| http.get('/index.html') } res['location']

更多推荐

在 Ruby 中获取 URL 的重定向

本文发布于:2023-10-10 12:05:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   Ruby   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!